Linux Basic Commands

Basic Commands

When you open a terminal window in linux you will automatically be in your home directory. To print your current directory



To go up one directory

cd ..


To go back home from whatever directory you are type cd



List directory


Long listing of directory

ls -l

Long listing including hidden files

ls -la

Long list directories only

ls -ld

Switch user


Add a user called des with a home folder /home/des

useradd -m -d /home/des des

Apply or change the password for a user account

passwd des

List users who are logged in to your machine


Check the id number of a user

id des

To count the number of files in freds home directory we can pipe ( | ) the results of ls -l into a wc -l command which counts the number of lines

ls -l /home/fred | wc -l

You could also use wc -l to count the number of lines ina text file

cat mytextfile | wc -l



We can use redirection to send the output of one command to a file. In this example we use sort with a dlimeter of a comma (,) listing numerical column 3 of the stats.txt and storing it in a new file sortedstats.txt

sort -t, -k3n stats.txt > sortedstats.txt

where -t, = field separator of comma  and -k3n = column 3 numerical


To see a list of the processes running on your machine use the ps command. Pipe it into  more

ps -ef | more

To find all the processes running under user des

ps -ef | grep des

Top will provide a real time view of processes running with some summary infomation.


If you find a process you would like to terminate, say PID 2056 you can use the kill command.

kill 2056

If this does not terminate the process you can force a kill with the -9 option

kill -9 2056

To find out what PID the terminal widow you are using is type in the terminal window.

echo $$

For example open a terminal  and type sleep 600 and then open another terminal and find this process using grep

sleep 600

ps -ef | grep sleep


Group Definitions

To add a group edit the /etc/group file

vi /etc/group



mkdir /home/mygroup

chgrp mygroup /home/mygroup
ls -ld /home/mygroup

Give the group write permission
chmod g+w /home/mygroup 

Turn on set group id on mygroup so all files created in it have group id permissions  

chmod g+s /home/mygroupman chmod

ls -ld /home/mygroup

umask 2 (change umask to 2 to enable write for group automatically)

Find files and directories

To find a file named hostname ignoring case and searching from /

find / -iname hostname

To find all files named zulu and redirect the output into a file named zulufind ignoring errors

find / -name Zulu>zulufind 2>/dev/null

To find a file from the current directory

find . -name passwd

To ignore errors add 2>/dev/null to the end where 2 means stderror and >/dev/null redirects to a null device (basically dumps the errors to nowhere)

find / -name passwd 2>/dev/null

Batch jobs using at

Change to root user in SuSE

su -

In ubuntu use sudo before every administrative task. To install packages inubuntu use sudo apt-get install packagename

sudo apt-get install atd

To schedule a task using at, first make sure you have the service running on you machine.

service atd status

If its not installed, for SuSE you use zypper

zypper in atd

If it's not running start the service

service atd start

Make it start every time the machine boots

chkconfig atd on

To copy your home folder to a backup directory at 23:00 at night as root user type at and  and then the command

at 23:00

Then once you have the at> prompt type your command

at>cp -R /home /backups



Backups and Archives using tar and gunzip

To make an archive we use the tar command which stands for tape archive. To make an archive of my home directory /home/des, cd to it and type

tar -cvf mytar.tar /home/des/

This will create the archive file mytar.tar in /home/des The -cvf stands for -c = create v = verbose and -f = file archive

To see a list of the contents of the tar file

tar -tvf /home/des/mytar.tar

To compress the tar file, cd  to the directory of the tar file and type

gzip  mytar.tar

This changes the tar file to a tar.gz compressed archive

To uncompress the tar.gz back to a tar

gunzip mytar.tar.gz

And to extract the archive, cd to the directory you want the files in

tar xvf  /home/des/mytar.tar

You can also use a shorcut to uncompress and unpack a tar.gz in one command

gunzip -c mytar.tar.gz | tar xopf -

You can use the short version of tar and gzip which makes a .tgz compressed archive. To archive and compress a folder named des, cd to it directory and use:

tar cvfz  des.tgz des

This creates a compressed archive named des.tgz. To extract the contents you can use:

tar xvzf des.tgz

This extracts the contents of des.tgz into the current directory.

If you want to extract into a specific directory then cd to that directory and use tar. For example if I wnat to untar into desdump directory.

mkdir desdump

cd desdump

tar xvfz ../des.tgz 

Where ../des.tgz is the relative path to the .tgz file. You could also use the full path to the file like /home/des/des.tgz

Find out what version of SuSE linux you have

Linux versions are stored in the /etc directory under a file named XXXX-release where xxxx is the linux version e.g SuSE, Ubuntu etc. So just cat /etc/xxxx-release

cat /etc/SuSE-release

To find the correct file

ls -l /etc | grep .*-release

Using Cron scheduler

Cron is a daemon that executes scheduled commands and is started automatically from /etc/init.d when multiuser run levels are started.

To list all cron jobs

crontab -l

To start a new crontab

crontab -e

Some systems will give you a choice of editor to edit the crontab file, if you want to set yours to a specific editor permanently type

export EDITOR=vi

And to set it for nano

export EDITOR=nano

Use the same process for your favorite editor

Once the file is open add a line containing the format below followed by the command you want to run.

Minute Hour Day of month Month Day Command

0-59    0-23        1-31         1-12    0-6  

0         20           13             *        6

For example to run a cron job command looking at the amount of disk space that is free(df -h)  at 11:30 *(every day of the month) * (every month) 1-5(day of week)

30 11 * * 1-5 df -h

To run every 20 mins from 7 - 5 on mon,wed,fri

0,20,40 7-17 * * 1,3,5 du -sh /var

Networking using arp, ssh, and scp

To list all machines in the address resolution table

arp -a

We can use the Secure Shell (SSH)  network protocol for secure data communication, remote command-line login, remote command execution. First make sure the service is running .

service sshd status

service sshd start

Make it run automatically

chkconfig sshd on

Add a user to another linux machine and ssh to that machine as that user

useradd -m des

Enter your password twice when asked

Now ssh to that machine

ssh des@

To secure copy the  folder myfolder over the network to /home/joe

scp -r des@ /home/joe

To copy the file passwd to a remote machine as newpw

scp /etc/passwd joe@