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 last 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 index
File 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 -1
To list files that have been modified within the last 3 days (within 72 hours):
find . -mtime -3
List files whose modification time is 1 day (24 hours) old or older:
find . -mtime +0
List files whose modification time is 4 days (48 hours) old or older:
find . -mtime +3
File 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 not very intuitive to me, and I regularly need to re-check them from 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.txt
If there are many changes, it is often useful to combine diff with less:
diff v1.txt v2.txt | less
Then 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 directory2
Sometimes 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