Basic Linux Commands

File Commands

ls

Displays directory listing

ls -al

Displays formatted listing with hidden files

cd

Change to home directory

cd dir

Change directory to  dir

pwd

Show current directory

mkdir dir

Create a directory  dir

rm file

Delete  file

rm -r dir

Delete directory  dir

rm -f file

Force remove  file

rm rf dir

Force remove directory  dir
Note: Use with extreme caution

cp file1 file2

Copy  file1 to  file2

cp -r dir1 dir2 

Copy  dir1 to  dir2
Creates  dir2 if it does not exist

mv file1 file2 

Rename or move  file1 to  file2

ln -s file link

Create symbolic link link to file

touch file

Create or update  file

cat > file

Places standard input into  file

more file

Outputs the content of  file

head file

Outputs the first 10 lines of  file

tail file

Outputs the last 10 lines of  file

tail -f file

Outputs the contents of  file at is grows, starting with the last 10 lines

 

 

Process Management

ps

Displays currently active processes

top

Display all running processes

kill pid

Kills process id  pid

killall proc*

Kills all processes named  proc
Note: Use with extreme caution

bg

Lists stopped or background jobs
Resume a stopped job in background 

fg

Brings most recent job to foreground

fg jobname

Brings job  jobname to foreground

 

 

File Permissions

chmod octal file

Change the permissions of  file to  octal , which can be found separately for user, group, and other by adding: 

  • 4 - read (r)
  • 2 - write (w)
  • 1 - execute (x)

To form the  octal add the values of r, w, or x. The first digit stands for the user, the second stands for the group, and the last digit stands for world.

Ex: chmod 777 file - read, write, execute (rwx) for all

Ex: chmod 755 file - rwx for user, rw for group and world

 

 

SSH

ssh user@host

Connect to  host as  user

ssh -p port user@host

Connect to  host on port  port as  user

ssh -copy -id user@host

Add you key to  host for  user to enable a keyed or passwordless login

 

 

Searching

grep pattern files

Search for  pattern in  files

grep -r pattern dir

Search recursively for  pattern in  dir

command | grep pattern

Search for  pattern in the output of  command

locate file

Find all instances of  file

 

 

System Info

date

Shows the current date and time

cal

Shows this month's calendar

uptime

Shows current uptime

w

Displays who is online

whoami

Displays who you are logged in as

finger user

Displays information about  user

uname -a

Shows kernel information

cat /proc/cpuinfo

cpu information

cat /proc/meminfo

memory information 

man command

Shows the manual for the  command

df

Shows disk usage

du

Shows directory space usage

free

Shows memory and swap usage

whereis app

Shows possible locations of  app

which app

Shows which  app will be run by default

 

 

Compression

tar cf file.tar files

Creates a tar named  file.tar containing  files

tar xf file.tar

Extract the files from  file.tar

tar czf file.tar.gz files

Creates a tar named  file.tar.gz containing  files with Gzip compression

tar xzf file.tar.gz

Extract the files from  file.tar.gz or file.tgz with Gzip

tar cjf file.tar.bz2 files

Creates a tar named  file.tar.gz containing  files with Bzip2 compression

tar xjf file.tar.bz2

Extract the files from  file.tar.bz2 with Bzip2

gzip file

Compresses  file and renames it to  file.gz

gzip -d file.gz

Decompresses  file.gz back to  file

 

 

Network

ping host

Pings  host and outputs results

whois domain

Gets whois information for  domain

dig domain

Gets DNS information for  domain

dig -x host

Reverse lookup  host

wget file

Downloads  file

wget -c file

Continue a stopped download

 

Installation

./configure
make
make install

 Install from source

dpkg -i pkg.deb

Install a package (Debian)

rpm -Uvh pkg.rpm

Install a package (RPM)

 

 

Shortcuts

Ctrl + C

Halts the current command

Ctrl + Z

Stops the current command

Ctrl + D

Log out of current session; similar to exit

Ctrl + W

Erases one word in the current line

Ctrl + U

Deletes from current cursor position to beginning of line

Ctrl + R

Type to bring in recent command

!!

Repeats the last command

exit

Log out of current session