Linux Commands
Contents
Directory commands
File commands
Redirection commands
Directory Commands
pwd command
Print full path of current directory:
> pwd
ls command
List the contents of the working directory:
> ls
List all contents including hidden files:
> ls -a
List detailed information for all files including permissions:
> ls -l
List files in the order they were modified:
> ls -t
Page through the contents of a directory:
> ls | more
mkdir command
Create a directory:
mkdir [directory name]
> mkdir example
cd command
Change to directory:
cd [directory name]
> cd example
Move up a directory:
> cd ..
Move up 2 directories:
> cd ../..
Move up to an adjacent directory:
cd ../[directory name]
> cd ../test
rmdir command
Delete a directory:
rmdir [directory name]
> rmdir example
File Commands
touch command
Create a file:
touch [file name]
> touch example.txt
rm command
Remove file:
rm [filename]
> rm example.txt
Delete a directory and its contents:
rm -r [directory name]
> rm -r example
cp command
Copy file and rename:
cp [src file] [dest file]
> cp filename1.txt filename2.txt
Copy file(s) into directory:
cp [filename …] [directory name]
> cp filename.txt dir1
Copy a directory including its contents into a new directory:
cp -R [directory] [new location]
> cp -R dir1 dir2
mv command
Move file to new location:
mv [filename …] [new location]
> mv filename.txt dir
Rename old filename to new filename:
mv [old filename] [new filename]
> mv filename.txt example.txt
Move a directory to a new location:
mv [directory] [new location]
> mv example dir1
cat command
Read file contents:
cat [filename]
> cat example.txt
more command
Show file contents one line (on enter) or one page (on space) at a time:
more [filename]
> more example.txt
less command
Navigate forward and backward in a file:
less [filename]
> less example.txt
Redirection Commands
output operator
Redirect standard output to a file and overwrite the contents:
output > filename
> cat filename1.txt > filename2.txt
append operator
Append standard output to filename:
output >> filename
> cat filename1.txt >> filename2.txt
input operator
Filename on the right is input into command on the left:
command < filename
> sort < filename.txt
pipe operator
The standard output on the left is piped as input into the command on the right:
output | command
> cat filename.txt | sort
sort command
Take standard input and sort it:
> cat filename.txt | sort > sorted.txt
uniq command
Filter out adjacent duplicate lines:
> sort filename.txt | uniq > unique.txt
grep command
Search files for lines that match a pattern and return result:
grep [text] [filename]
> grep apple filename.txt
sed command
Find and replace the first occurrence of a string in a file:
sed -i 's/[searchString]/[replacementString]/' [filename]
> sed 's/apple/banana/' filename.txt