Linux Basics #2 - Commands

Last Edited: 10/17/2024

This blog post introduces some basic commands in bash.

DevOps

In the last article, we discussed what the OS, kernel, and shell are, and we set up our environment. From this article, we will go over some useful commands for interacting with the shell. In particular, we will cover commands in bash (Bourne Again Shell), which is the default command-line interpreter in almost all Linux distributions. Some of you might already be familiar with bash commands, but I recommend reviewing them as there may be flags, shortcuts, wildcards, and commands you weren't aware of. Also, note that some commands may not be available in the Docker container we set up.

Linux treats everything as files in a file system, and being able to navigate and manage files is essential. Below are some basic bash commands related to files:

File-Related Commands
CommandUsageSide Note
pwdPrint current working directoryN/A
cdChange directoryYou can either specify an absolute path from the root or a relative path from the current directory. You also have access to shortcuts like cd . (current directory), cd .. (parent directory), cd ~ (home directory), and cd - (previous directory).
lsList directoriesYou can specify a path to see a list of directories. Hidden files that start with . are not visible unless you use the -a (all) flag. You can also display more information by using the -l (long) flag, which can be combined with -a as -la to show everything.
touchCreate new file or update timestampUse it like touch newfile to create an empty newfile. You can also specify an already existing file to update the timestamp (visible with ls -l).
fileCheck file typeLinux doesn't require strict correspondence between file extension and file type. To check the file type, use file example.jpg.
catRead file contentAbbreviation of concatenate, useful for viewing the short content of a file. You can also concatenate the contents of multiple files by specifying them with spaces between.
lessView a large text fileDisplays the file in a paged manner. Navigate the file using keyboard commands (q to quit, arrow keys for previous/next pages, g/G to go to the beginning/end, /<word> to search for a word, and h for help).
cpCopy filesUse cp <from> <to> to copy a file. You can use wildcards like * (for multiple characters), ? (for a single character), and [<chars>] (for character ranges) to copy multiple files. To copy multiple files or directories, add the -r (recursive) flag. To avoid accidental overwrites, use the -i (interactive) flag.
mvMove or rename filesUse mv <filename> <newfilename> to rename files, or mv <filename> <directory> to move a file to a different directory. Use the -i flag to avoid overwriting files or the -b (backup) flag to automatically rename the existing file with a ~.
mkdirMake new directoryCreate a new directory by listing its name after the command. You can also create a subdirectory with the -p (parent) flag like mkdir -p grandparent/parent/child.
rmRemove filesDeleted files cannot be recovered. To delete a write-protected file without a warning, use the -f (force) flag. Other options include -i (for interactive warnings) and -r (to remove entire directories). You can also use rmdir to remove directories.
findFind files or directoriesUse find <directory> -name <filename> to search for a file by name. To find a directory, use find <directory> -type d -name <directory>.

These are some of the most commonly used commands related to file navigation and management. Even if you are familiar with these commands, it’s worth reviewing the notes to see if there are any additional details or features you didn’t know about. The shortcuts and wildcards introduced here can be used in other commands as well. We will discuss the Linux file system in more detail in a future article.

Standard Streams

As the CLI operates on text, the ability to manipulate text is extremely important. The echo command is like the print function, which takes standard input (stdin) and returns standard output (stdout). When you use echo Hello World, it takes the stdin from the keyboard and returns Hello World to stdout, which by default is the screen.

Instead of outputting to the screen, you can redirect stdout to a text file using the redirection operator > like this: echo Hello World > example.txt. However, this will overwrite any existing text in example.txt. To append text instead of overwriting, you can use >>. The default stdin can also be set to a text file using <, like echo < example.txt. You can use these operators to set both stdin and stdout to text files and copy content between them, for example: echo < example1.txt > example2.txt. If you’re following the C++ series on the blog, you may have seen << and >>, which are designed to imitate this behavior.

For stderr, you cannot use the redirection operator as is. Instead, you need to use file descriptors, which are 0, 1, and 2 for stdin, stdout, and stderr respectively. For example, you can log an error (like trying to access a non-existent directory) to a text file using ls fake 2> log.txt. If you want to log both stdout and stderr, you can do so using &>, like this: command &> log.txt.

You can also use the stdout from one process as the stdin for another process, instead of outputting to the screen or a text file, by using the pipe operator |. This allows you to do something like ls -la /long_directory | less to inspect long outputs with less. You can combine the pipe operator with tee to write the output of a command to two different streams, for example: ls -l | tee example.txt, which displays the result on the screen while also writing it to a text file.

Working with Text Files

After logging the stdout and stderr of commands to a text file, you may want to manipulate and investigate the text file. Below are some basic bash commands related to text files.

Text File Related Commands
CommandUsageSide Note
cutExtract contentYou can extract content by a list of characters using the -c (character) flag, like cut -c 5 example.txt, or by fields using the -f (field) flag, like cut -f 2 example.txt. Special characters are included, and everything separated by a TAB is considered a field by default. To change the delimiter, you can use the -d (delimiter) flag, like cut -f 2 -d ";" example.txt.
pasteMerge linesYou can merge lines into one line, separating them with TABs, using paste -s example.txt. You can also merge multiple files by listing multiple text files and create a new file from them using the redirection operator. The -d flag can be used to change the delimiter from TAB.
headDisplay first n linesThe default number of lines displayed is 10, but you can change that with the -n (number) flag, like head -n 15 long.txt.
tailDisplay last n linesWorks similarly to head. You can use the -f (follow) flag to track changes made to the text file. To quit the process, type Ctrl+C.
expandDisplay TABs as spacesThe content of the text file is displayed with TABs replaced by spaces. You can use the redirection operator to store the text in a new file. The unexpand command can convert spaces back into TABs.
joinJoin multiple files by a common fieldWhen each line of text files has common fields (like an ID) separated by a space, we can join them with join. You can specify which field to use by using flags like -1 and -2 to indicate the file and the field number, like join -1 2 -2 1 example1.txt example2.txt. You can also split a file into different files with the split command.
sortSort linesThis command sorts lines in alphabetical order by default. You can reverse the order with the -r (reverse) flag or use numerical values for sorting with the -n (number) flag.
trTranslateYou can translate one character set in stdin into another, like tr a-z A-Z to get HELLO. You can delete characters with the -d flag.
uniqRemove adjacent duplicate linesYou can remove adjacent duplicates of lines in a text file with uniq. You can also obtain a count with the -c (count) flag, show only unique lines with the -u (unique) flag, or show only duplicated lines with the -d (duplicate) flag. To remove non-adjacent duplicates, combine it with sort, like sort example.txt | uniq.
wcDisplay word countDisplays the number of lines, words, and bytes in a text file. You can display specific counts with -l (line), -w (word), and -c (character) flags. You can also use the nl command to add line numbers to a text file.
grepSearch using regular expressionsgrep (global regular expression print) finds text patterns in a file using regular expressions. You can use it like grep hello example.txt. You can make the search case-insensitive with the -i (insensitive) flag and combine it with other commands using pipes.

There are many other commands related to text files, but the above are the basic ones you’ll use frequently. The grep command, in particular, is one of the most commonly used and worth looking into. As mentioned, you can combine these commands with redirection and piping, which offers flexibility that GUI tools often cannot match.

Other Useful Commands

In addition to the commands introduced for navigating directories and manipulating text files, there are a few others that are important and useful.

Other Useful Commands
CommandUsageSide Note
helpDisplay help for commandsProvides useful information about other bash commands, such as available flags and a short description of the commands. Use it like help echo.
manManualProvides an in-depth manual on how commands can be used. You can alternatively use whatis for a shorter description.
aliasCreate alias of commandsSome commands are long but frequently used. You can create shortcuts by assigning a new name to a command using alias, like alias foobar='ls -la'. You can undo an alias with unalias foobar.
historyShow command historyDisplays the history of commands you've used. You can also use clear to clear the screen.
envShow environment variablesVarious environment variables can be viewed with commands like echo $HOME and echo $USER, and you can view them all with env.

When you want to rerun a recent but lengthy command, you can use the up/down arrow keys to retrieve previous/next commands from your history. There are many other interesting commands and shortcuts that can boost your productivity, which I may introduce in future articles.

Conclusion

In this article, we’ve covered essential commands related to navigating files and manipulating text files. While we didn’t cover every command or concept, these will be addressed in future articles. Even if it feels like a lot, you’ll quickly get used to these commands by practicing. If you're unsure about a command while trying it, use help and revisit this article.

Exercises

This is an exercise section where you can test your understanding of the material introduced in the article. I highly recommend solving these questions by yourself after reading the main part of the article. You can click on each question to see its answer.

Resources

  • Linux Journey. n.d. Kernel. Linux Journey.