File Operations Using the Command Line in Linux
Articles and examples about processing files using the command line.
File Search Using the find Command
I use the find command frequently in programming and all kinds of file handling tasks. Typically I use it to search files based on filename or partial filename. Another common use case is when I want to see where the most recently modified files are, or to check which files I have modified today or in the last 30 days, etc.
Command for Listing All Files
The following command lists all files in the current directory and its subdirectories:
find .Searching for Files By Name
We can use the grep command to filter text so that only lines containing a specific string are preserved. We can use the pipe character to combine the find and grep commands. With a combination we can build e.g. a file search.
This command searches the current directory (including subdirectories) for files that include the string "index" in the filename. The search is case-insensitive. It can find files such as "INDEX.html" and "Index.Txt".
find . | grep -i indexFile Search Based on Modification Time
This command lists all files in the working directory (including subdirectories) that have been modified within the last day (within 24 hours):
find . -mtime -1To list files that have been modified within the last 3 days (within 72 hours):
find . -mtime -3List files whose modification time is 1 day (24 hours) old or older:
find . -mtime +0List files whose modification time is 4 days (48 hours) old or older:
find . -mtime +3Adding More Information to Results
You can add information to find's output using the -printf parameter. The following command searches for files modified during the last 7 days and adds the precise modification time and file path to the output.
find . -mtime -7 -printf "%T+ %h/%f\n"In this example, the modification time is printed at the beginning of each line in the format year-month-day etc., so we can put the list to chronological order by piping the results to the sort command:
find . -mtime -7 -printf "%T+ %h/%f\n" | sortFile Search Using Other Conditions
You can use the find command for at least a hundred other purposes, too. For example, you can search by ctime or atime value, file owner or group, file size, or file permissions. The man page (man find) lists all the parameters. Here I wanted to write examples of the mtime parameter especially, because the precise meanings of its plus and minus parameters are hard to remember.
Other Operations
Find can also be used for manipulating files. More information is available in the "ACTIONS" section of the man page.
Comparing Files and Directories
Suppose we have a text file v1.txt. We make a copy of it, naming the copy as v2.txt. Then we edit the latter file. If we want to check what edits have been made, we can find it out using the diff command. It gives a listing that tells what words or rows have been added, removed, or changed.
An example of using the diff command:
diff v1.txt v2.txtIf there are many changes, it is often useful to combine diff with less:
diff v1.txt v2.txt | lessThen the list of changes open in the less program where we can scroll the list using arrow up and arrow down keys. We can close the less program and return to command line pressing the key q.
To compare directories:
diff -r directory1 directory2Sometimes we only want a short file-level list of differences between directories. The following tells, what files have been added, removed, or changed:
diff -qr directory1 directory2