Useful Commands for Ubuntu Linux

User/Permission

sudo <command>

sudo = Super-User-DO

Run command as root user

sudo su

su = Super User/Switch User

To open a shell as another user (including root)

su - user2

To switch user to user2

whoami

To get current user name

chmod

changes the file mode (permission) of each given file/folder

sudo chmod 777 abc.sh

chown

change file owner and group

sudo chown username:group directory

change all file and folders recursively

sudo chown -R username:group directory

File/Directory

ls

List the contents of the folder

ls -la

list in long listing fashion + hidden files

mkdir

To create a new directory

cd path

to change directory.

cd /home/techbrij/projects

to go in parent directory

cd ..

to go in user home directory

cd ~

touch

create file if not exists, update timestamp if file exists.

touch myfilename.sh

cat

displays a file content

cat pq.txt

displays a file content with line numbers

cat -n pq.txt

join two or more plain file

cat p.txt q.txt >> pq.txt

cp [options] source dest

to copy files and directories.

cp myFile.txt /home/techbrij/

recursive copy (including hidden files):

cp -R devFolder bakFolder

mv [options] source dest

Move all txt files in current directory to subdirectory bak

mv *.txt bak

Rename directory brij to techbrij:

mv brij techbrij

tail [options] [filenames]

By default, tail returns the final ten lines of the file

tail techbrij.txt

to print the final 15 lines

tail -15 techbrij.txt

System/Application

date

print the current date and time

to set date time

date --set='21 may 2016 13:57'

ps

shows all of the processes associated with the current user and terminal session

To get a more complete picture of the processes

ps aux

To search the process list

ps -ef | grep searchkeyword

ps -ef | grep node

pgrep

to get the PID of a process

pgrep bash

pgrep -f 'ssh example.com'

kill <pid>

to kill process

kill -9 2405

kill -9 'pgrep ping'

nohup command-name &

To run command in background

nohup myscript.sh &

apt

apt = Advanced Package Tool

To install myPackage

apt-get install myPackage

To update packages

apt-get update

tar

To create tar.gz archive file

tar cvzf Myfile.tar.gz /home/techbrij

To untar tar file or uncompress tar.gz file

tar -xvf Myfile.tar

to list the content of tar.gz file.

tar -tvf Myfile.tar.gz

Network

ifconfig

show network information

iwconfig

show wireless information

ssh

To securely log onto remote systems

ssh remote_host

ssh remote_username@remote_host

ssh -p port_number remote_host

To start ssh agent and set environment variable

eval "$(ssh-agent -s)"

ssh-add ~/.ssh/id_rsa

To copy files in secure way

scp source_file user@destination_host:destination_path

scp techbrij.txt root@xxx.xxx.xxx.xxx:/home/techbrij

if destination host access is already configured

scp techbrij.txt xxx.xxx.xxx.xxx:~/

to copy folder recursively

scp -r folder destination

scp -r /home/techbrij/ root@xxx.xxx.xxx.xxx:~/

Shortcuts

Paste in terminal

Shift+ Insert

To open terminal

Ctrl+ Alt + T

To switch apps

Alt + Tab

Switch between windows from the same application

Alt + `

Pop up command window

Alt+F2

Lock the screen

Ctrl + Alt + L

Others

clear

Clears the current screen

which <commandname>

To find the path of the application

which svn

which node

nautilus

To open file explorer

history

prints the history of executed commands in terminal

pwd

pwd = print working directory

md5sum

To verify integrity of files

md5sum myfilename.deb

;