I want to add tags to my files (in this case to PDF files) so I can search for them in the filesystem and then process the result from the command-line or in a script.

Is there a linux-tool that can do this for files in general?

An Easy way would be to modify the filenames and then access those with find -exec or in pipe Though I want to tag the files with multible tags, and the filenames would get to long, but I want to process them in this kind of way

For an example, lets say I have plenty of PDF files. So I want to tag some of them as bills, some of them as drafts

So that later I could make an application browse through my filesystem and process all the matches. Lets say create symlinks for all of them in an appropriate folder, Or merge them to one single PDF etc...

My question is not about those programms that would come second in the pipe as: ln, gs, pdfjoin, but about those working with the tags directly such as: applying the tags and searching for files containing those tags.

  • 1
    Do you want the tags to be in the PDF files (and so if you move the files or send them to somebody, the tags will still be present, but if you regenerate the PDF file the tags will be lost), or do you want to leave the PDF files unmodified (in which case the tags will be associated with a particular location, and will be preserved if you update the file but not if you move it)? Aug 2 '17 at 22:00
  • @Gilles This is a really good question! and I really have to think about it. In the first place I would be interested in both options. It would be ace if I could do it this or the other way but with a search I could match both. Well, I use PDF files mainly compiled by LaTex, so I would pick your second option and preserve the tags when the PDFs get recompiled.
    – nath
    Aug 2 '17 at 23:22
  • Some further thoughts: lets say for the second option (I don't know the technical aspects) but it sounds like the tags do not get saved in the file but in a database file that contains the tags and links to the tagged file. So when the file is moved somewhere the link gets broken and so the tags. But wasn't it possible, to use a sed like tool on the database file to fix those links? Maybe even a script that does that, in a way that the tagged file is moved with the script that can then automatically fix the broken link in the database. (maybe this thoughts are a bit theoretical... )
    – nath
    Aug 2 '17 at 23:27

This isn't quite a match for what you're thinking, but if working with files that support metadata is of interest, exiftool can view and change the metadata on a large number of file types, including PDF files. For a full list, see man exiftool.

I've used it to create and change metadata on PDFs on numerous occasions. For example:

  exiftool -Title="My PDF" \
           -Subject="stuff" \
           -Description="my pdf about various things" \
           -Keywords="miscellanea, nonsense" \
           -Author="me" \
           -Creator="also me" \

Now here's where it becomes more closely related to your idea. The Keywords metadata field (or any other field for those file formats which support the creation of arbitrary fields - many do) can be used to store your tags in the files themselves, allowing the tag symlink farm to be automatically maintained by a script.

Alternatively, a script could maintain a database (flat-text like CSV or similar, or an SQL database like sqlite) containing a list of filenames (with full absolute path), filesystem metadata (timestamps, size, perms, etc) and their tags. Other scripts could be written to search this database and return the result(s) in a useful format.

For example:

vi $(search-tagged-files --date "last sunday" --keywords thesis)


localc $(search-tagged-files --keywords budget,2017 \

NOTE: the single biggest drawback to anything like this is the enormous amount of work it would take to maintain the tags for each of the files. Some of this could be automated, but much of it would be tedious, time-consuming manual work. And that's ignoring the design and development time to come up with a system to do it with.

None of the programs used to create or edit files would be in any way integrated with a file management system like this, and neither would standard tools like mv or cp or rm.

You could write wrapper scripts for many of them that were aware of this tags database and updated it automatically, but I wouldn't even know where to begin doing that if you used a GUI file browser to move, copy, open files etc...you'd probably have to write your own file browser.

The work involved is probably the biggest reason why most people who have had ideas like this have ended up thinking "I'll just use a well-organised directory tree instead". Even the work required to write the code to manage the documents is is enormous, and the work to manage the metadata for each file is even larger - it's generally only worth the effort for very large organisations with at least tens of thousands of documents to keep track of.

This isn't a new idea, there's been a lot of research and development on ideas like this. One of the names for it is Document Management System.

  • First of all: thank you cas! this is exactly an answer I was hoping to get. I would love to maybe collect a few more, while I'm going to read into man exiftool and play around with its options... I can't upvote yet, I will do as soon as I can :-) Your examples at the end of your answer are pretty new to me, since I never really used vi or localc, would you mind to add some further explanation to those?
    – nath
    Aug 3 '17 at 21:45
  • they're just examples/ideas of using a (hypothetical) search script to return a list of filenames. vi's a text editor, and localc is the Libre Office spreadsheet. $(...) is called command substitution and is a way of using the output of one program (the search script) as the arguments to another (vi or localc or whatever)
    – cas
    Aug 3 '17 at 22:59
  • I know what you mean when you talk about: "the single biggest drawback to anything like this is the enormous amount of work it would take". I think a procedure like tagging has to fit into the work-flow, like using git add ... or git commit ... I did not get into it as far enough to decide how the use is going to be for me, but your information and also the other comments have been quite a good initiation to go into this subject a little deeper.
    – nath
    Aug 4 '17 at 0:44

In my experience, attempting to tag files using metadata can be unreliable. Not all file managers allow you to view or modify metadata directly, and I've had metadata not carry over to different systems or not show up in the same way on Windows as it does on Linux or vice-versa.

Personally, I've taken an approach similar to how the program TagSpaces handles it; I add the tags to the filename itself.

TagSpaces does it this way: Filename [tag1 tag2 tag3].ext

I do it this way: Filename #tag1 #tag2 #tag3.ext

I've found this to be very effective, especially coupled with a program like catfish (which is very similar to Everything for Windows) tied to a hotkey. Since every tag is begun with a #, if I search for the tag #bills I don't have to worry about returning files with the word "bills" in their name that don't have that tag.

I've spent many hours investigating different methods of organizing my files with tags or tag-like systems, from using services like Evernote to complicated hierarchies of folders. This is the easiest, most seamless method I've been able to find, and is OS-independent.

  • Thank you for sharing your experiences. I have been working like this too, when I have been working lots with audio files on windows. In the long run as stated in my question I wasn't very happy with the quite long filenames. In this "Windows-days" I have been tagging files at the beginning of the filename with [tag]_filename.ext. I completely given up on this on a Linux system. The advantage of this method was that the main tag (I have been usually not using more than one for a file) sorted the files. So if I had all files in one directory they where automatically sorted.
    – nath
    Aug 6 '17 at 15:28
  • @nath When you say you've given up on this on a Linux system, do you mean that since switching to Linux you've decided not to do it this way, or is there something about Linux that makes this method not work for you? Aug 6 '17 at 15:31
  • Thanks for asking for this detail, the comment section is sometimes to short to be to specific. There is several reasons that I have given up on this method but they are not mainly related to linux other then the fact that here I try to avoid characters like ()[] but certainly most special characters and I wrote a tiny script to step by step make my file-system not contain spaces anymore. The reason was rather that before I was producing music and tagged my bounced audio files this way. Since I'm on Linux I mainly work with lilypond scores and there was no need for this so far.
    – nath
    Aug 6 '17 at 15:42
  • @nath I suppose you could consider prepending your tags with a word instead of a special character (e.g. "filename tag_bills.ext"), but by that point it might be too difficult to read the actual filename. Aug 6 '17 at 15:46
  • Earlier on windows I also made my kind of revision-controll by adding numbers and numbers and numbers to my files to always be able to revert changes. But my filenames where sometimes horribly long. Since I switched to linux and do most work from the command-line this is not really an option and anyway now I work with git. But I also spend quite an amount of time to manage my file-system and change directory trees. A nice thing that came with linux is using folders containing symlinks, but for the automation of this work-flow my interest in tags rised again, but I never found anything really.
    – nath
    Aug 6 '17 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.