Linux provides lots of commands for finding, counting, and renaming files. Here's a look at some useful choices. Credit: GotCredit Linux provides a wide variety of commands for working with files — commands that can save you time and make your work a lot less tedious. Finding files When you’re looking for files, the find command is probably going to be the first command to come to mind, but sometimes a well-crafted ls command works even better. Want to remember what you called that script you were working on last night before you fled the office and drove home? Easy! Use an ls command with the -ltr options. The last files listed will be the ones most recently created or updated. $ ls -ltr ~/bin | tail -3 -rwx------ 1 shs shs 229 Sep 22 19:37 checkCPU -rwx------ 1 shs shs 285 Sep 22 19:37 ff -rwxrw-r-- 1 shs shs 1629 Sep 22 19:37 test2 A command like this one will list only the files that were updated today: $ ls -al --time-style=+%D | grep `date +%D` drwxr-xr-x 60 shs shs 69632 09/23/19 . drwxrwxr-x 2 shs shs 8052736 09/23/19 bin -rw-rw-r-- 1 shs shs 506 09/23/19 stats If the files you’re looking for might not be in the current directory, the find command is going to provide better options than ls, but it can also result in a lot more output than you want to peruse. In this command, we’re avoiding searching in directories that do not begin with dots (many of those get updates all the time), specifying that we want to find files (i.e., not directories) and requesting that we only be shown files that were updated within the last day (-mtime -1). $ find . -not -path '*/.*' -type f -mtime -1 -ls 917517 0 -rwxrw-r-- 1 shs shs 683 Sep 23 11:00 ./newscript Notice how the -not option reverses the -path specification, so our search doesn’t dive into subdirectories that begin with dots. If you want to find only the largest files and directories, you can use a command like this du command that lists the contents of the current directory by size. Pipe the output to tail to see only the largest few. $ du -kx | egrep -v "./.+/" | sort -n | tail -5 918984 ./reports 1053980 ./notes 1217932 ./.cache 31470204 ./photos 39771212 . The -k option gets du to list file sizes in blocks, while x keeps it from traversing directories that are on other file systems (e.g., referenced through symbolic links). The fact that the du listing starts with the file sizes allows the sort by size (sort -n) to work. Counting files Counting files in any particular directory is fairly easy with the find command. You just have to remember that find will recurse into subdirectories and will count the files in those subdirectories along with those in the current directory. In this command, we are counting files in one particular user’s home directory. Depending on permissions on home directories, this may require the use of sudo. Remember that the first argument is the starting point for the search — in this case, the specified user’s home directory. $ find ~username -type f 2>/dev/null | wc -l 35624 Note that we’re sending error output from the find command above to the bit bucket to avoid trying to search directories like ~username/.cache that we likely cannot search and the content of which is probably not of interest. When needed, you can constrain find to a single directory using the maxdepth 1 option: $ find /home/shs -maxdepth 1 -type f | wc -l 387 Renaming files Files are easy to rename with the mv command, but sometimes you will want to rename large collections of files and likely won’t want to spend a lot of time doing it. To change all the blanks that you might find in file names in the current directory to underscores, for example, you could use a command like this: $ rename 's/ /_/g' * The g in this command, as you likely suspect, means “global.” That means the command will change all blanks in a file name to underscores, not just the first one. To drop the .txt extension from text files, you could use a command like this: $ rename 's/.txt//g' * Wrap-up The Linux command line provides a lot of useful options for manipulating files. Please suggest other commands that you find especially useful. Related content how-to How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe