These are the basic commands that will help you move around and manage your file system from inside the Linux terminal. Before going over any of the commands; though, these are some variables/operators you should know first. This lets us shorthand common things to type and chain commands together in powerful ways.
*
The wildcard. This can represent any text when searching. For example: if you want to search for all photos you could use the wildcard followed by jpg, png, etc. *.png
|
The pipe. This is a very handy operator that lets us take the output from one command and give it straight to the second command following the pipe.
&
The single ampersand. If your command starts a process, appending the ampersand to it will run the process in the background instead of occupying your terminal.
&&
The double ampersand. This operator will only execute the second command following the operator if the first one was successful.
.
The period. This is a shortcut to the current working directory, so you don't have to type out the whole path to where you are.
;
The semicolon. Run many commands in one line if they are separated by this operator. This lets them run independently of each other.
>
This operator takes the output of a command and overwrites the following file with that output.
>>
Similar to the single greater than, but it will append to the file instead of overwriting it.
HELP
--help
A more concise version of the man command that prints to the terminal instead of entering another interface. This command is used as a flag to other commands.
Usage: cat --help
man
The man command is used to pull up the manual on other commands. The manual will be a text interface over the terminal, and can be exited by inputting q. This will have a summary information about usage of the command as well as information about all flags that can be appended to the command. This is also where you can find any arguments a command may take.
Usage: man ls
info
A more detailed version of the manual. This page often includes examples as well as more detailed descriptions.
Usage: info cat
FILE NAVIGATION
ls
This command lists children of the current directory. By default, it does not display hidden files, but they can be toggled on with the -a flag. By default this command will only display the files’ name, but more info can be displayed with the -l flag. This will show user’s privileges on a file such as read, write, and execute privileges, and other info like size.
Usage: ls -l
cd
This command changes the current working directory. It takes one argument of a file path. Use cd .. to move back a directory.
Usage: cd photos
pwd
Print Working Directory. This one is self explanatory. It prints the path to the current working directory.
Usage: pwd
cat
Concatenate files and print to the terminal. Ideal for quickly printing the contents of smaller files.
Usage: cat file.txt
less
Less is like cat but for larger files. Will print the files in pages, so they can be viewed one at a time. Use space to go forward a page and backspace to go backwards. q to quit.
Usage: less file.txt
tail
Quickly print the last few lines in a file to the terminal. This is good to view the end of a file without leaving the CLI.
Usage: tail file.txt
head
Quickly print the first few lines in a file to the terminal. This is good to view the beginning of a file without leaving the CLI.
Usage: head file.txt
grep
This command prints lines from a file where text matching an input pattern is found. The first argument after any option flags or switches is the pattern. The pattern may be a string or regular expression. The second argument will be the files to be searched. Zero or more may be input. The file argument is optional because grep can also be chained with the pipe operator to search that output instead of a file. Grep is especially useful when used like that! The -v flag will reverse grep to print every line not including an input pattern.
Usage: grep “install” README.md
find
Find files and directories based on various criteria. The first argument will specify the directory to start the search in, and the following arguments will be the criteria to search for. For example, we can search for -type f for just files or -type d for just directories. We can also search by name with -name “cat.jpeg”.
Usage: find . -type f -name “*.pdf”
which
This command returns all executable files for a shell command. Packages like unzip, nmap, docker, etc. that have a shell command are located within a directory, and the exact location of the file that executes when these commands run can be found with which.
Usage: which docker
whereis
The whereis command can locate the binary, source, and manual page for a command.
Usage: whereis docker
FILE MANAGEMENT
touch
Touch makes a file. Many file names can be input to make multiple files at once. Touch can also edit the timestamps on files that already exist.
Usage: touch file1.txt file2.txt
mkdir
This command will make a new directory if it does not already exist. Many directory arguments may be passed in to make multiple directories. Permissions may be set with the -m flag.
Usage: mkdir photos
rm
Removes a file. This command cannot remove directories by default. The -r flag must be used for directories, indicating recursive removal. Note: files removed with rm can usually be recovered. Use shred for more assurance that a file will be unrecoverable.
Usage: rm -r photos
cp
Copies files or directories to a target destination. Typically, two arguments are passed in with the first being the source to be copied and the second being the destination to be copied to. The -t flag can flip the order where the first argument is a directory and the following arguments are files to be copied into it.
Usage: cp -t photos cat.jpeg dog.jpeg
mv
Moves or renames files or directories. If two file names are given, the first will be moved to the second, effectively renaming it. The -t flag works similarly to the cp command.
Usage: mv -t photos cat.jpeg dog.jpeg
This was a basic overview of how to start using commands to interact with your Linux file system. There are many more commands to Linux built in and even more that can be installed through packages.
Happy coding!
Top comments (0)