In the Linux command line, working with files is made easy by the multitude of file-manipulation commands that are available.
For each task you need to perform, there is a solution; for many, there is more than one solution.
Among the commands that we’re exploring today — which are all, in one way or another, file-related — you might not only find what you’re looking for, but also something that you never knew you wanted to look for!
Although the availability of commands may vary between different distributions of Linux, most of these are standard enough that they should work for you.
The cat command, which is short for concatenate, is used to display, combine and/or copy files. It can even create new files.
At its simplest,
cat filename will print out — or display — the contents of the specified file.
To copy the contents of one file into another,
cat oldfile.txt > newfile.txt can be used, but with caution. If the new file does not exist it will be created; if it already exists it will be over-written!
To combine the contents of two files (the contents of the first file will be appended to the bottom of the second file), use
cat oldfile.txt >> newfile.txt.
The dd command converts and copies data. It is not necessarily an easy tool for command line beginners to learn, and has been nicknamed the “destroy/delete data” command due to its destructive tendencies when misused. For these reasons, we’ll leave this one alone for now!
The echo command will print out any text that is is given. Example:
echo this is cool but seems useless
By using the -e option and enclosing the text in single quotes, you can use utilize sequences such as new lines in your text. Example:
echo -e 'this is on\nseveral lines'
The fgrep command is similar to the grep command (see below) but does not support regular expressions, making it faster (which may not noticeable to some of us!)
The file command can be used to determine a file’s type.
The syntax is
The find command searches for files in your file system, from the command line.
If you use
find without qualifiers (location, filename, string, etc.) you will start to get a list of every file on your file system. This would be a good time to use the Ctrl + C keyboard shortcut to terminate the action, unless you have extra time to spare!
Otherwise, to be more specific, you can specify the location to search in, and/or the exact file name or part of the file name to search for.
find ., for example, will search for all files in the current directory.
find /home/username will search for all files in your home directory.
find -name filename.txt will search for all files with the specified file name. This is useful because the whole path will be returned, allowing you to see exactly where the file can be found.
find /home/username -name '*txt*' will search for all files and directories in your home directory with ‘txt’ as a part of their name. The asterisks (*) are called wild cards, and they’re awesome. We’ll explore them in more detail later.
There are a multitude of other examples I could use, but since we don’t have time for them in this article, you’ll had to start experimenting on your own until we can go into more detail.
The grep command is used to search inside a file or files for a specific string or pattern. It will, by default, print out the lines that match the query.
The syntax is simply a case of using the command, followed by the string to search for, and then the file to search in. Example:
grep "find me" filename.txt
The use of regular expressions (a subject too complex to get into right now) opens up a multitude of new possibilities, but in the meantime, check out the available options/arguments. There are some handy ones!
The less command is a newer version of the more command, with additional features. It allows you to view files from the command line. The most basic syntax is
The ln command can link files together. It can made hard links and also soft links (which are similar to shortcuts).
The locate command finds files by name. In other words, its a quick and easy way to find files by specifying filename or part of a filename. The results, including the full path to the file, will be returned. Example:
The lpr command will send a specified file or files to be printed — by an actual printer.
The more command allows you to browse through the contents of a file from the command line. There are many options available — check out the man page for details. The most basic syntax is
The nl command will number the lines of a file by printing out the file, with the line number preceding each line. Example:
The od command displays a file’s contents in octal format, or the base-8 number system. Various arguments allow for the file to be displayed in other formats. Example:
sed is a stream editor for filtering and transforming text. Unless otherwise specified, sed will print its output on the screen instead of saving it.
The stat command will display the status of a file or a file system. You will be able to see the size, access/modify/change dates, and other information. Example:
The strings command will print any strings of printable (non-binary) characters in a file.
The touch command changes file time-stamps (access and modification times), as previously explained.
The wc command will print out the number of lines, words and characters (or bytes) in a specified file. Example:
It may be that we later go into more detail on several of the most popular file-related commands listed here.
In the meantime, check out their man pages for additional information, and to learn what options/arguments are available.
You can start using them right away!