Linux Commands

Basic Linux Commands

PWD

Prints working directory

pwd
/home/user

CD

Changes directory

cd 
cd ~ # Will take you home
cd ../ # Will move you up one directory
cd ../../ # Will move you up two directories
cd / # Will take you to root

LS

Lists directory contents

ls
ls -a # Produces directory structure as well
ls -al # Produces comprehensive list of directory files and permissions

RM

Removes a file or folder

rm
rm filename.txt # Removal of a file
rmdir -r /DirectoryName # Forces removal of directory and all files

MKDIR

Creates a new folder / directory

mkdir DirectoryName

CAT

Displays contents of a file

cat testFile.txt

ECHO

Prints out text to terminal, or additionally to a file

echo "My String" # My string
echo "My String" > temp.txt # Writes My String to temp.txt file
cat temp.txt # My String

echo "My new string" >> temp.txt # Appends content of text to file
cat temp.txt # My String My new string

MV

Moves a file

mkdir NewFolder # Creates a new folder named NewFolder
echo "My Text" > NewFile.txt # Creates NewFile.txt with your content
mv NewFile.txt NewFolder # Moves your textfile to your folder

WHOAMI

Displays the current active user

whoami

MAN

Manual, displays information on commands

man ls

WHICH

Displays which and where the specified program is installed

which python3
# /usr/bin/python3

ALIAS

Creates a macro shortcut command

alias temp='ls -a'
temp
# . .. NewFolder
ls -a
# . .. NewFolder

Advanced Linux Commands

STDERR

Print error message to a specified file

echo "hello" 2> log.txt # No Errors, does not print anything
echo NotAFunction 2>> log.txt # Appends NotAFunction: command not found

STDOUT

Prints output to a specified file

echo "hello" > log.txt 2>&1 # Direct any error messages into file

FIND

Find a file by type

find / -type f -name log.txt 2>/dev/null 
# Find type file with name log.txt
# If there are any errors, direct them to null to be deleted

TEE

Print output of a command and store the output into a file

ls | tee output.txt

CUT

Access particular byte of data in a file

cat log.txt 
# hi
cat log.txt | cut -c 1
# h
cat log.txt | cut -c 2
# i

NL

Display file with line numbers

nl log.txt
# 1 hi
# 2 hello

Display content of a file starting at top

head -3 log.txt

TAIL

Display content of a file starting from bottom

tail -10 log.txt

SORT

Sort the contents of a file

sort -r log.txt # Reverses order of contents
sort log.txt | uniq # Removes duplicate content

WC

Display content information about a file

wc log.txt 
# 7 7 59 log.txt
# Number of Lines | Number of Words | Bytes or Characters | File Name

wc -l log.txt
# 7 log.txt
# Number of lines | File Name

FILE

Displays information about a file

file log.txt
# log.txt: ASCII text

GREP

Match a pattern of a string to search file content

cat log.txt | grep "hello" # Returns only content that contains "hello"
grep "hello" log.txt # Performs the same

Last updated