This document contains details on how to use several SHELL commands with basic usage examples. Commands can be executed with various arguments, and flags, for example, the ls command can be used with the -l flag like so ls -l to list the files in your present working directory in long form. There are several other flags. If you ever want to look at documentation for a command and all its possible flags, you can do so in 3 ways:
- using the
-hoption - using the
--helpoption - using the
mancommand
Example:
ls -hls --helpman ls
If you use the third option, a rather long, truncated document will appear on your terminal. You can press the 'Enter' key to continue scrolling down the documenttaion, or you can press the "q" key to quit the documentation viewer and return to your regular termianl prompt screen.
Go to a directory
cd path/of/directoryGo to home directory of the current user
cdGo up one directory
cd ..Go to the previously choosen directory (if no directory was choosen before, chooses the current directory and goes to home directory)
cd -Examples: Go to the lectureNotes directory using four different ways (assuming we are in the home directory)
cd Documents/lectureNotes
cd ./Documents/lectureNotes
cd ~/Documents/lectureNotes
cd /home/$USER/Documents/lectureNotesLists the contents of the current working directory
lsLists [a]ll files, including hidden files
ls -aLists files [R]ecursively, that is also lists files that are in subdirectories and their subdirectories and so on
ls -RLists files in [l]ong listing format, shows permission, file size and modification date
ls -lExamples: Lists all files in long format with human-readable file size format
ls -lahLists files in long format recursively
ls -lRCreates a directory called newDirectory in the current working directory
mkdir newDirectoryCreates a directories called newDirectory1 newDirectory2 in the current working directory
mkdir newDirectory1 newDirectory2Creates directories recursively
mkdir -p path/to/newDirectoryCreates a file named filename.txt
touch filename.txtCreates files named filename1.txt, filename2.txt, filename3.txt
touch filename{1,2,3}.txtCopies a file to the target location
cp path/to/source path/to/targetCopies a file to the target location [R]ecursively
cp -R path/to/source path/to/targetMoves a file to the target location
mv path/to/source path/to/targetRenames name1.txt to name2.txt
mv same/path/name1.txt same/path/name2.txtMoves a file to the target location, prompts for confirmation before overwriting existing files
mv -i path/to/source path/to/targetRemoves files
rm path/to/file1 /path/to/file2Recursively remove a directory and all its subdirectories
rm -r path/to/directoryForcibly remove a directory and all its subdirectories without showing error messages
rm -rf path/to/directoryGive the [u]ser who owns a file the right to e[x]ecute it
chmod u+x path/to/fileGive [a]ll users rights to [r]ead and e[x]ecute
chmod a+rx path/to/fileChange permissions recursively giving [g]roup and [o]thers the ability to [w]rite
chmod -R g+w,o+w path/to/directoryGive all users rights to read, write and execute
chmod 777 path/to/fileRemove e[x]ecutable rights from the [g]roup
chmod g-x path/to/fileRemove all rights from [o]thers
chmod o= path/to/filePrints Hello World to the terminal
echo "Hello World"Appends Hello World to hello.txt, creates the hello.txt if the file does not exist
echo "Hello World" >> hello.txtPrints the contents of hello.txt
cat hello.txtAppends contents of hello1.txt and hello2.txt to combinedHello.txt
cat hello1.txt hello2.txt >> combinedHello.txtSearch for a pattern in a file
grep "search-pattern" path/to/fileSearch for an exact string in a file
grep --fixed-strings "exact-string" path/to/filePrint file name and line number for each match
grep --with-filename --line-number "search-pattern" path/to/fileSearch for a pattern in all files recursively in a directory, showing line numbers of matches, ignoring binary files
grep --recursive --line-number "search-pattern" path/to/directory