Linux: Finding Files


  • Service Provider

    Finding the location of an unknown file on Linux can be found in numerous ways but the most important of which is the find command. Using the find command we can search through the filesystem hierarchy looking for a file my name, modification date or other factor. Using the find command can take a little practice, but it relatively easy and very powerful once learned.

    We will jump right in looking for some basic usage examples:

    find / -name myfile.txt
    

    The above example searches through the entire system, starting at the filesystem root "/" looking for a file with the name "myfilt.txt". The syntax here is in the format of find "starting from /" "a file by the name of" "myfile.txt".

    We could easily modify this command to not search through so many locations, but to limit to more practical ones such as the /home directory or the current working directory (as shown by the pwd command) in these two examples:

    find /home -name myfile.txt
    

    Or...

    find . -name myfile.txt
    

    Searching can involve several directories in a single command:

    find /opt /var /home -name anotherfile.txt
    

    You can also put standard regex into the filename which can be important, for example, if you do not remember the extension name like so:

    find / -name myfile*
    

    The find command can do a lot more than look for the name of a file. It is often used to look for files that have been modified at a certain time or that have other specific properties. We will look at a few of the more common search types here. The man page for find will show the extensive list of possibilities for file searching.

    Some options for search types other than by name include:

    • amin search by access time in minutes (file was accessed n minutes ago.)
    • atime search by access time in days
    • ctime search by changed time in days
    • group search for files belonging to a group
    • iname search by name, same as name but not case sensitive (i = insensitive)
    • inum search by inode number.
    • mtime search by modification time in days.
    • user search by files owned by user.

    There are many, many more search options which we will not go into detail with here. But the find command is very powerful and will let us dig very deeply into our system to find files in myriad ways. It is one of the more powerful and useful tools that we use on the Unix command line.


    Part of a series on Linux Systems Administration by Scott Alan Miller



  • How about finding and renaming files that contain an invalid Character for Windows?
    For example, a filename that ends in a Space or a period.


  • Service Provider

    @flabbergastedFiltch said in Linux: Finding Files:

    How about finding and renaming files that contain an invalid Character for Windows?
    For example, a filename that ends in a Space or a period.

    Like one that came from Windows and now is on Linux and you want to fix it?

    Finding files that end in a period is pretty easy.

    find /directory -name *.
    

    The *. regex is "anything" followed by a period.



  • I don't know if you plan to do a bit on this or not, but one way of searching for things I find very handy is listing all files that contain a certain text string...

    grep -r -l 'find this' *

  • Service Provider

    @travisdh1 said in Linux: Finding Files:

    I don't know if you plan to do a bit on this or not, but one way of searching for things I find very handy is listing all files that contain a certain text string...

    grep -r -l 'find this' *
    

    Yup, will be getting to grep and stuff :)



  • Yea, Scott's reply led me to:

     find . -name "*\ " -print
    

    Not a big leap.

    Now I'm all the way down the rabbit hole of xargs, grep, and perhaps sed (?)

    Using FreeBSD on EMC Isilon.

    I have to do this a lot, but the creative way users name files is ever changing:
    filename.
    filename. [filename dot space]
    file*name

    Here's a sample of a real directory:

    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEULIGCONOBL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUBLA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._TIMESEXTBOL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._GILLSANBOL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/GILLSANEXTBOL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVETICA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVETICANEUE3.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUHEACONOBL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUULTLIG.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUROM.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._ZAPFDINGBATS.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVEBOLOBL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVETICANEUE1.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._TIMESBOLITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVETICANEUE2.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEULIGCON.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUBLA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUHEACON.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVETICANEUECONDENSED3.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/GILLSANITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUMEDCONOBL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._TIMESSEMITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUTHI.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVETICANEUE3.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/TIMESBOLITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVETICANEUE1.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/TIMESEXTBOL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUHEACONOBL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUHEAITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/ZAPFDINGBATS.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUULTLIG.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUBLAITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUMEDCON.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/GILLSANLIGITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUHEAITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUTHI.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUHEA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUMEDCONOBL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUBLAITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUHEA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUTHIITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUMEDITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEULIGITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._GILLSANITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._GILLSANEXTBOL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._GILLSANBOLITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEULIGCON.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEULIGITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/TIMESSEMITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVEBOLOBL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUMEDCON.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUMEDITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUBOL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUULTLIGITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/GILLSANLIG.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUULTLIGITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._GILLSANLIG.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVETICA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/GILLSANBOLITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEULIG.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUMED.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._GILLSANLIGITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUHEACON.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUMED.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVETICANEUE2.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEULIGCONOBL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/GILLSANBOL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUTHIITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVETICANEUECONDENSED3.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUBOL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEULIG.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUROM.

    One or Two files (even 10) and I'd use mv to rename the files and remove the space or period or add extension, etc... This list represents just one folder out of a few hundred that have some gremlin preventing Windows NT Backup (don't ask) from taking a backup and deleting the originals.

    Thanks for the reply and nudge. Now back to it after I change my Job Description on Linkedin to "Frustrated wannabe Software Developer".


  • Service Provider

    I need to do a second post about -exec and such, too.



  • @flabbergastedFiltch said in Linux: Finding Files:

    Yea, Scott's reply led me to:

     find . -name "*\ " -print
    

    Not a big leap.

    Now I'm all the way down the rabbit hole of xargs, grep, and perhaps sed (?)

    Using FreeBSD on EMC Isilon.

    I have to do this a lot, but the creative way users name files is ever changing:
    filename.
    filename. [filename dot space]
    file*name

    Here's a sample of a real directory:

    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEULIGCONOBL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUBLA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._TIMESEXTBOL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._GILLSANBOL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/GILLSANEXTBOL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVETICA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVETICANEUE3.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUHEACONOBL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUULTLIG.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUROM.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._ZAPFDINGBATS.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVEBOLOBL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVETICANEUE1.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._TIMESBOLITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVETICANEUE2.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEULIGCON.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUBLA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUHEACON.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVETICANEUECONDENSED3.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/GILLSANITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUMEDCONOBL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._TIMESSEMITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUTHI.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVETICANEUE3.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/TIMESBOLITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVETICANEUE1.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/TIMESEXTBOL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUHEACONOBL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUHEAITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/ZAPFDINGBATS.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUULTLIG.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUBLAITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUMEDCON.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/GILLSANLIGITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUHEAITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUTHI.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUHEA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUMEDCONOBL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUBLAITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUHEA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUTHIITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUMEDITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEULIGITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._GILLSANITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._GILLSANEXTBOL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._GILLSANBOLITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEULIGCON.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEULIGITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/TIMESSEMITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVEBOLOBL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUMEDCON.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUMEDITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUBOL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUULTLIGITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/GILLSANLIG.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUULTLIGITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._GILLSANLIG.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVETICA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/GILLSANBOLITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEULIG.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUMED.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._GILLSANLIGITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUHEACON.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUMED.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVETICANEUE2.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEULIGCONOBL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/GILLSANBOL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUTHIITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVETICANEUECONDENSED3.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/._HELVENEUITA.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUBOL.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEULIG.
    ./WebUploads/FAMILYANDFRIENDS.1/b_Fonts/HELVENEUROM.

    One or Two files (even 10) and I'd use mv to rename the files and remove the space or period or add extension, etc... This list represents just one folder out of a few hundred that have some gremlin preventing Windows NT Backup (don't ask) from taking a backup and deleting the originals.

    Thanks for the reply and nudge. Now back to it after I change my Job Description on Linkedin to "Frustrated wannabe Software Developer".

    As much as I like giant one liners, the best tool for this would be rename.

    To remove a period just do

    find . -type f -exec rename "." "" {} \;
    

    You don't technically need to use find, but if the files span multiple directories it's easier.



Looks like your connection to MangoLassi was lost, please wait while we try to reconnect.