Friday, October 16, 2009

Linux essential commands

Linux essential commands

Kernal / OS related commands

1. To know the version/flavor of Linux:
cat /proc/version
cat /etc/issue

2. system information
uname (  --kernel-release, --kernel-version, --all )

Storage and memory related

Hard disk

1. To know the amount of space (hard disk) utilized:
df -h
Similarly by using the du command, we can see the amount of disk space used for a set of files or directories
du -sh /home
This gives total disk space used by the /home directory

2. Clear duplicate files and broken symlinks
FSlint is a utility to find and clean various forms of lint on a filesystem, especially duplicate files and broken symlinks.

sudo apt-get install fslint

3. Display amount of free and used memory in the system

free (Default displays in KB)

free -m (to display in MB) free -g (to display in GB)

4. Find Out Hard Disk Specs:
# sudo hdparm -I /dev/sda


1. View the most CPU intensive processes currently running


Package Management

1. Cleaning up of partial package, apt cache, unused dependencies

sudo apt-get autoclean

sudo apt-get clean

sudo apt-get autoremove

2. Remove Orphaned Package

sudo apt-get install gtkorphan

3. To Search packages
sudo apt-cache search dos2unix

4. The following command creates a list of all the installed packages at the present time
sudo dpkg --get-selections > /etc/package.selections


When you are working on a large program, it can be difficult to determine which modules need to be recompiled because of their dependency relationships. The make utility automates this process. make looks at dependency lines in a file named Makefile in the working directory and constructs the target. A simple makefile has the following syntax:

target: prerequisite-list
TAB construction-commands

Note: TAB is keyboard tab

The first ine is dependency line, where the targest is the name of the file that depends on the files listed in the prerequisite-list. construction-commands are regular shell commands that construct target file.


form: size.o length.o
     gcc -o form size.o length.o
size.o: sice.c form.h
     gcc -c size.c
length.o: length.c form.h
     gcc -c length.c

Macros in Make

Syntax of a macro definition is


$ cat Makefile
# Make example

form: size.o length.o

     $(CC) form size.o length.o
size.o size.c form.h
     $(CC) -c size.c

This way if you want to change the compiler, you just have to change the line CC=gcc.

Manual or help related commands

1. Get help on general or system commands, tools, libraries etc.

2. Quick reference commands

3. Quick shell reference

4. Related to programming languages
    perldoc, javadoc etc.

and .. no Google is not a linux command!

4. type -a svn (It will dispaly all the execution files ...) like

5. lsb_release -a
which will print distribution specific information

Users and group management

1. Create new group
   addgroup group1
   After creating a group the system allocates a new group id to the group.

2. Create new user
adduser --home /home/test --shell /bin/bash --ingroup testgroup testuser
adduser -h /home/test -s /bin/bash testuser testgroup
After creating the new user, the system allocates a new user id to the user

3. id
prints  the current user's id

4. See all the users or groups
   cat /etc/passwd
   cat /etc/group

5. Change user's attributes
    usermod -a -G admin testuser
        (this command adds the testuser to the admin group)
        (-a stands for append. If it is not given, the existing groups of testuser will be completely ignored, and testuser will just become a member of only admin group)

6. Change password

Session or terminal specific

1. See the users logged in


2. Find what is the current terminal


3. Clear current terminal setting

  tput reset

4. Empties the current screen



Shutdown Command

shutdown -h now


Find and list

1. To Find the Folders is a Folder

 ls -l  | grep ^d

2. To find older files, and perform some action:

find . -name "*.bz2" -mtime +10 -exec rm -f {} \;

The above command finds all the bz2 files which are older than 10 days, and delete them.

Shell Script

To access arguments individually use $1, $2 etc. To access all the arguments in one shot use $@.
There is one more way of accessing the arguments  - $* - the difference between $* and $@ is that one allows quoted arguments whereas the other
does not.

Another command from the same family is shift .. this can be used to access the arguments using $1, $2 etc.

Internet or Web

WVDial (Setting up mobile gprs with computer)

1. Do "wvdialconf /etc/wvdial.conf" which will create a default configuration
2. Update "Phone" according to your mobile provider value
3. Just run "wvdial"


If you want to download all the files in a specific directory of a url, use wget with the options "-r" and "-np".
"-r" stands for recurseive download.
"-np" stand for no-parrent. If you dont use this option, and just use "-r", then wget downloads all the diretories and files from the base url irrespective of the exact directory you mention.
E.g.: wget -r -np

No comments: