Skip to content

Latest commit

 

History

History
185 lines (179 loc) · 4.78 KB

File metadata and controls

185 lines (179 loc) · 4.78 KB

Shell Cheatsheet

Quick tips:

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 -h option
  • using the --help option
  • using the man command

Example:

  • ls -h
  • ls --help
  • man 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.

Change Directory

Go to a directory

cd path/of/directory

Go to home directory of the current user

cd

Go 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/lectureNotes

List Contents of a Directory

Lists the contents of the current working directory

ls

Lists [a]ll files, including hidden files

ls -a

Lists files [R]ecursively, that is also lists files that are in subdirectories and their subdirectories and so on

ls -R

Lists files in [l]ong listing format, shows permission, file size and modification date

ls -l

Examples: Lists all files in long format with human-readable file size format

ls -lah

Lists files in long format recursively

ls -lR

Create Directory

Creates a directory called newDirectory in the current working directory

mkdir newDirectory

Creates a directories called newDirectory1 newDirectory2 in the current working directory

mkdir newDirectory1 newDirectory2

Creates directories recursively

mkdir -p path/to/newDirectory

Create File

Creates a file named filename.txt

touch filename.txt

Creates files named filename1.txt, filename2.txt, filename3.txt

touch filename{1,2,3}.txt

Copy File

Copies a file to the target location

cp path/to/source path/to/target

Copies a file to the target location [R]ecursively

cp -R path/to/source path/to/target

Move or Rename a File

Moves a file to the target location

mv path/to/source path/to/target

Renames name1.txt to name2.txt

mv same/path/name1.txt same/path/name2.txt

Moves a file to the target location, prompts for confirmation before overwriting existing files

mv -i path/to/source path/to/target

Delete a File

Removes files

rm path/to/file1 /path/to/file2

Recursively remove a directory and all its subdirectories

rm -r path/to/directory

Forcibly remove a directory and all its subdirectories without showing error messages

rm -rf path/to/directory

Change Permissions of a File

Give the [u]ser who owns a file the right to e[x]ecute it

chmod u+x path/to/file

Give [a]ll users rights to [r]ead and e[x]ecute

chmod a+rx path/to/file

Change permissions recursively giving [g]roup and [o]thers the ability to [w]rite

chmod -R g+w,o+w path/to/directory

Give all users rights to read, write and execute

chmod 777 path/to/file

Remove e[x]ecutable rights from the [g]roup

chmod g-x path/to/file

Remove all rights from [o]thers

chmod o= path/to/file

Print to Terminal

Prints 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.txt

Print Contents of a File (Concatenate)

Prints the contents of hello.txt

cat hello.txt

Appends contents of hello1.txt and hello2.txt to combinedHello.txt

cat hello1.txt hello2.txt >> combinedHello.txt

Find Patterns in Text

Search for a pattern in a file

grep "search-pattern" path/to/file

Search for an exact string in a file

grep --fixed-strings "exact-string" path/to/file

Print file name and line number for each match

grep --with-filename --line-number "search-pattern" path/to/file

Search 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