Essential Commands
Overview
Linux has a hierarchy of directories that lists contents of files in a tree-like format, starting from the file system root (
/).Linux is case-sensitive, e.g.
Myfile,myfile,MYFILE, andMyFileare four unique files.Linux does not require filename extensions such as
.doc,.exe, and.abc. The.extension is simply part of the filename. Sometimes data files are named with extensions (.hdf, .cdf, .tar) for human readability, though this is optional.A file with
.at the beginning will be considered a hidden file.You can use the
<TAB>key to autocomplete commands, paths, and environment variables. For example, you can typecalon your terminal followed by<TAB>to test this. If there is more than one option for the autocomplete to choose from, pressing<TAB>twice will provide a list of all possible options based on what you have typed.The
up-arrowwill display the previous command you have typed and if you press thedown-arrow, it will refer to the following command.The
historycommand will show all of the previous commands you have entered during the last session(s).
Common Shortcuts
macOS keyboard
Windows keyboard
Action
CMD+D
CTRL+D
Exit a terminal, same as typing exit
CMD+L
CTRL+L
Clears the screen, same as typing clear
CMD+C
CTRL+C
Breaks/cancels an ongoing operation
CMD+Z
CTRL+Z
Pauses (stops) an ongoing operation
CMD+N
CTRL+N
Opens a new terminal
📝 Note: If you want to learn more shortcuts, please consult more documentation here.
Basic Commands
pwd: Print Current Working Directory
pwd /Users/userThe output of
pwdin this case, is the home directory of the user user, which is shown with the complete path starting from root(/)ls: List the contents of the current directory
ls Documents/ Pictures/ Desktop/ Downloads/ document.txtThe output is a list of four directories (followed by a
/) and one file. To see information about the contents in a list, typels -l.cd: Change directory
cd DocumentsIn this case, we are entering the "Documents" directory.
If you want to go directly to your home directory (user), you can type
cdwithout any specification of which directory.In the case of nested folders, you can jump one directory level upwards by typing
cd ..alias: In case of deeply nested folders (/path/to/project/com/java/lang/morefiles) that might take more than 4 directory levels upwards, you can create an
alias, for example,alias ..2="cd ../.."oralias ..3="cd ../../.."oralias ..4="cd ../../../..". If you wish to make these aliases a permanent feature of your Bash environment, you may add the commands to the end of the.bashrcfile. Edit the.bashrcfile by opening it in your favorite text editor (it is located in your home directory). For example, typevi .bashrc.whoami: Shows the user ID as a name
whoami userThis shows the username that is logged in to the current session of the machine.
If you need additional information about the user, such as, to which groups they are a member, type
id.If you want to see all the users that are logged in to the computer, you can type
w.date: Display the date and time of the system
date Wed Apr 4 09:06:30 EDT 2018The date is shown in a complex format. Use
date +%Fformat if you want to do a backup of a file including the date in the filename.If you want to calculate, in seconds, the duration of a program, you can use the
date +%scommand.cal: Display a calendar of the current month
calThis command displays the calendar of the current month of the year in which the command is executed.
In case you need the whole year calendar of 2018, you may type
cal 2018or set any other year you want to check.If you want to display any particular month of the year, you can type, for example,
cal March 2018.To display the Eastern date of the current year, please type
ncal -o.cat: Creates a single or multiple files, views the contents of a file, concatenates files, and redirects output into the terminal or into files
cat /Users/user/myfile.txt hello worldIn this case, we want to display the content of
myfile.txtwhich is located inside theuserdirectory.If we are positioned inside the
userdirectory, all that is needed iscat myfile.txtto see its contents, which is "hello world".You can view the content of two files at the same time with the
cat file1.txt file2.txt.In case you need the lines of a text numbered, please type
cat -n myfile.txt.echo: Display a line of text or a string on standard output or into a file
echo "Hi CCLA" Hi CCLAIn this example, the string
Hi CCLAis shown because we send that message to the terminal.To view the value assigned to a variable, add
$before the variable name:(e.g.
x=10; echo "The value of 'x' is: $x").If you need a new line
\n, use the option-e(e.g.echo -e "Hello \n world").touch: Create a new empty file
touch myNEWfileIn this case,
myNEWfilewas created inside the directory in which you are positioned.You can create more than one file at the same time with by typing
touch file1 file2.If you want to create lots of files that share a common string, e.g.
test1.txt,test2.txt,test3.txt, and so on until 25, you can usetouch test{1..25}.txt.mkdir: make directory
mkdir myNEWdirIn this case, a new directory called
myNEWdiris created in the current path.If you want to set the permission of the directory while you are creating the directory, you can do so by typing
mkdir -m a=rwx myNEWdir. Here, the letters r, w, and x stand for read, write, and execute, respectively. For more information on file and directory permissions, see here.If you want to create multiple directories at once, run
mkdir test1 test2 test3.If you want to create several subdirectories at one time, type
mkdir -p /home/test/test1/test2/test3/test4.cp: Copy files and directories
cp /path/to/file_src /path/to/file_destIn this case the contents of
file_src(source) will be copied tofile_dest(destination) and both files will be present in both paths.If you need to copy more than one file into a directory, you can type
cp main.c def.h /Users/user/mydir/.To copy all the files you have (in your current path) with the extension
.cto a directory calledbak, you can typecp *.c bak. The asterisk (*) is a wild-card character.mv: Move or rename the files or directories
mv file1 Myfile1The file called
file1was renamed asMyfile1.If you want to move all of your C files to a subdirectory called
bak, you can runmv *.c bak.If you want to create a backup when copying your
.txtfiles into themybakdirectory (to not overwrite existing files withinmybak) use:mv -bv *.txt /Users/user/mybak.rm: Delete files or directories
rm file1 Myfile1The files called
file1andMyfile1will be removed.For directories, the recursive option
-ris needed, e.g.rm -r modelOutput.man: Display the manual of the Linux commands
man sudoA manual related to the
sudocommand is displayed explaining how thesudocommand will grant you privileges to execute commands as the superuser does.For further information you can do
man manto read more aboutman. To exit a manual page, typeq.
Pipe and Redirection
| Pipeline
A pipe is a form of redirection that sends the output of a program (written before the pipe) to another one (written after the pipe) for further processing.
To make a pipe, put a vertical bar (
|) on the command line between two commands.man pipe | cat > /tmp/myMAN.txtThe command
man pipewill display the content of all the information about pipe, then that content will be processed bycat(taken as its input) and be redirected to the file/tmp/myMAN.txt. So, the output, the content ofmyMAN.txtwill display the manual information about pipe.> Redirecting output
Commands can send and receive streams of data to and from files and devices.
echo "Test report title" > /tmp/test.txt"Test report title" will be written to the file
test.txtlocated inside the/tmpdirectory.It is also possible to send all the content of
/tmp/hi.txtto/Users/user/hello, by using/tmp/hi.txt > /Users/user/hello.Mail -s "Subject" to-address@example.com < Filenamewill email the content ofFilename.>> Appending (postpending) redirected output
This command will append (postpend) information to where it is designated.
echo "This report was done at $HOSTNAME at $(date+%F)">>/tmp/report.txtThe output of the first part of the command (before the
>>) will be added at the end of the file/tmp/report.txt.
Last updated