Important Linux Commands for Developers

Here are few Linux/Unix commands useful for developers in day-to-day work. If you worked on Linux system then you may have used these commands but you may just be very basic on those command to stand out. You may need a little brush up with commands so that you can stand out in your Linux experience. Generally these days developers work on GUI mode and need to learn only few commands, and still they do learn few commands but cannot remember those commands. So, lets practice few commands so that we can remember those and we know those in some details.

Few Linux commands will not have a short description and others will have description with examples and use cases. Those without examples and use cases can be important at times but not frequently so better to keep just keep in mind.

at - executes a shell script at the specified time. Use atq to show pending jobs, and atrm to remove jobs from the queue. 'at' is like crontab.
$at -m 01:35 < atjob

It run the commands listed in the atjob file at time 1:35 AM and email to the user running the command all the output generated. After entering the command, you will get output similar to this - commands will be executed using /bin/csh job 10711111111.a at Tue Jan 20 ..
$at -l
It will list each of the scheduled jobs.

$at -r 10711111111.a
Deletes the job just created.

"Garbled time" error will come if you enter the command without time or any switch.

cat - combine, copy standard input to standard output. Used to join or display files.

Examples 1:
Enter the command at command prompt and after that press enter. A new file named newfile.txt will be created. After that enter your text. Once you add your text in a line and press enter you cannot go back to previous line for editing. After your text added, Press CTRL+D to save the file.

$cat > newfile.txt

CTRL + D to save the file at last.

Example 2:
$cat > filename
Simple way of creating file. It provides few options compared to vi or other editors but is always available.

Example 3:
cat file1 file2 > file3
content of file1 and file2 will be combined and added to file3.

Example 4:
$cat file4 >> file5
file4 content will be appended to file file5. It will NOT overwrite content of file.

Example 5:
Redirect content out to device/printer or modem
$cat /etc/motd > /dev/modem

$> newfilename - create a blank file named newfilename. I find it shortest command to create a file. Another command to create a blank file is $cat > newfilename

chown - Change ownership of a file.

Examples:
$chown - userid files
$chown -R userid files

cut - select characters or TAB-separated fields from lines of input and writes them to standard output.

Example Data:
r2c1: r1c2: r1c4: r1c5
r2c1: r1c2: r1c4: r1c5
r2c1: r1c2: r1c4: r1c5
r2c1: r1c2: r1c4: r1c5
r2c1: r1c2: r1c4: r1c5

For 'cut' command, each column means a character.
$cut -c1-6 company.data
Print just 4th and 8th cols of each line
$cut -c1,8 company.data
Our data is separated by colon:
$cut -d: -f3 company.data
where : is our delimiter and -f3 means third field.

(if you want to use space as delimiter then use quote -d' ')

egrep - search files for lines that match regular expression. Runs faster than grep and fgrep.

$egrep -v "^$" test.php

This will show all the lines after removing blank lines from file content. If your file has blank lines in between text/paragraphs then check with $cat test.php to see the difference. -v option tells egrep to print all lines that do not contain specified regular expression.

$egrep -v "^#" /etc/syslog.conf | grep -v "^$"

Prints all lines that are not a comment lines and also that are not a blank lines.

$egrep -i 'Name' test.php

$egrep -ic 'Name' test.php

-c is used for count. It will just tell the match count.

Search recursively:

$egrep -r "192.168.1.123" /etc/

Search words only:

$egrep -w 'word1|word2' /path/2/file

$egrep -ni 'Name' test.php

It will add the line number at the start of each matching line.

Output:

2: echo 'My name is: ';
8: echo 'What is the name of nearby collage';
10: echo 'His name is Satya Prakash';

grep/egrep command is often used with pipes.
Display cpu model name:

$cat /proc/cpuinfo | grep -i 'Model'

grep will take output that comes from 1st command. If you want to know the name of files with matching search terms instead of matching lines then

$grep -li 'search term' *.php
Output:
test.php
test2.php

You can display matching items in color:

$grep -i --color 'search term' *.php

useradd/adduser - For adding user. Can be used by root user only. adduser is an alias to useradd. It may not be available on many.

alias - assign name to specified command list. It is a shell built-in command.

alien - Utility to convert to and from different Linux package formats. It can handle Debian (deb), Stampede (stp), Red Hat (rpm), and slackware (tgz) package.

apropos - display command name based on keyboard search.
$apropos head
ar - create, modify and extract from archives.

aspell - spell checking program along the lines of ispell.

cd - change working directory.

chgrp - changes group associated with file.
Example: $chgrp groupName filesToApply
$chgrp -R group files

chmod - set permission (mode) of files or directory.

clear - clear screen

cp - copies one or more files. Recursive copying is one simple way of archiving part of directory structure.
$cp -r /sourceDir /targetDir

cpio - direct copy of files to an output device, allows creation of archive file spanning multiple disk. Allows one directory structure to be mirrored elsewhere on the partition or another partition. In order to backup entire directory structure on disk, cd to the dir and use the following command:
$find . -depth -print | cpio -ov > /dev/fdo

I have not tried above command. See the source of commands at the bottom.

csplit - separate files into sections. See also split.

date - display date
Example: #date
#date 'your date'

df - display capacity and free capacity on different physical devices.

diff - compare two files and display diff.
Example: #diff file1 file2

du - disk usage

Example : $du / -bh |less

dmesg - displays message from /var/log relative to the most recent boot.

env - display environment variable set. Information regarding home dir, path, shell prompt and others can be find or set.
$env | less

env allow you to run an application in different environment.

Just play a little with env command.
$export name=Satya
Added a variable name with value Satya.
Now, check the output of $env . You can do the same with $env | grep -i name
This must have shown you newly added variable. So, the newly added variable is added in your environment variable. You can check the exported variable using $echo $name.

Unset the exported variable using $unset name.

ex - interactive mode vi. It is like a option of vi/vim editor. Use vi -e for ex editor. There are other vi editor versions like view, gview etc. Find good pointer from here.

exec - system call which creates a subshell to execute a binary or a script.

exit - Exit a shell. Quit to exit from ftp mode.

expand - convert tabs in files to spaces and write to standard output.

Example:
To test this create a file with tabs in between words. Do this for two-three lines.
Now, run command
$expand urFile > urFile2
Go to urFile2 and tabs must be converted in spaces.

expect - a program that talks to other interactive problem according to script.

export - place the value of a variable in calling environment (and makes it global).

expr - evaluates an expression and displays the result.

fc - It is to view, execute, and edit commands for the history list. I could not make much sense of it usage by right now but got these two command useful.
Example:
$!! - it will run the last command you use.
$!vim - It will run the last vim command you entered. Suppose you run vim like this: $vim satyakaran.txt. Then this same command will run again. Syntax for this command is: $!str, where str is your command in the history. It will take last one.

file - displays classification of a file according to the type of data they contains.
Example:

$file test2.php
Output: test2.php ASCII english text

fdisk - used to partition hard drives.

find - display files according to a large variety of search criteria.

Find files using name -

$find -name "test.php"
it will find files in current directly. specify path to look for after find command name like this:
$find . -name "test.php"
Notice the dot after "find". This means current dir.

$find / -mindepth 3 -name passwd
Find the password file under root and two levels down. (root =1 level, and 2 sub directory).

$find -mindepth 3 -maxdepth 5 -name passwd
Find the password file between sub-dir level 2 and 4

Invert the match-
$find -maxdepth 1 -not -iname "test.php"
Find the non matching files in current directory.

Now, a fun problem to solve:

Q: Delete a file with special character. Suppose files file1.php, file2.php and file3.php and file?.php are there in a directory in system. Delete the file?.php from the directory.

Option 1: if we use 'rm' then it will delete all files as '?' represent any one character. So, the solution is not correct.
Option 2: First find the inode of the file:
$ls -il
Output is something like this:
804178 file1.php
902344 file?.php

Use the inode coming on first column to delete specific file.
$find -inum 902344 -exec rm '{}' \;

{} is quoted so that it doesn't got executed in first go. Run a command on each find match. {} will receive each match. semicolon is quoted with backslash.

- source: thegeekstuff.com

Find all empty files -

$find ~ -empty

There can be many situations like the above. I have practiced few of them. find command is solution for that. Check these questions:
find 5 top big files/small files
find file based on permission
find all dir. Solution: $find . -type d
find only normal files. solution: $find . -type f
Hidden files
$find . -type f -name ".*"
$find . -type d -name ".*"

ftp - file transfer over net.

grep - used to find a string in files.
$grep -r -H -I 'yourterm' /path/to/check

groupadd - create a new group on the system.
$groups - show which group you are in.

history - command for viewing and manipulating the shell command history list. Unlike DOS history, Linux history does not go after shell close. It does not go even after logout and login.

$history
$history | grep -i ls
get the information from history when you run list command "ls".

ifconfig - display network interfaces that are currently active.

kill - kill a process/job.

killall - kill processes by name. Kill all processes that are instances of the specified program.

killall5 - kill all processes except ones on which it depends.

last - generates a list of user logins.

less- Linux alternatives to 'more' command. 'more' also works. It displays text files one screenful at a time.

ln - create a link to a file. Used to create hard links and with the -s option, it creates symbolic links.

logout - logout. and bring login prompt.

ls - list dir content.
Options:

-l = long listing
-a = all files in dir. including hidden.
-R = recursive
-s = size of the file in blocks
-h = size in human readable format.

$ls -1
Do not you like files in multiple column! The above command will show you one file per line.
$ls -t | head -1
Latest modified file. $ls -t will sort it using modified time. head -1 will be get the first from the output.
$ls -l
filetypePermisson | links | owner | group | size | lmod.DateTime| FileName
links is number of links for that file. fileTypePermisson can be -, d, s, l (normal, dir, socket file, linkFile)

$ls -ltr
-t = last modified first
-r = reverse by last modified entry
$ls -I
display file inode number
$ls -F
Visual classification of file. Dir/ will come for directory. nothing for normal file, @ for link file, and * for executable file.

man - display info from linux reference manual. Try $man ls.

mkdir - create a directory/folder. Try:
$mkdir test
$mkdir /MentionAnyPath/DirToCreate.

mount - mount device.
$mount -a - will mount all file systems according to the specification in /etc/fstab.

mv - moves file / rename file. So, if you just want to rename file then:
$mv aFile.php bFile.php
aFile.php will become bFile.php.

Mention the new path and filename for moving the file to other place.

passwd - change password.
$passwd
Change you own password.
$passwd userName - if you are root user then you can change other user's password.

pr - Paginate file for printing.

pwd - present working directory.

rcp - Remote copy. This requires .rhost at remote system which will have user with access privileges.

reboot - Reboot system. Same as $shutdown -r

rm - remove files and directory. With -r option you delete recursively.

rmdir - Remove empty directory.

sed - A stream editor. It edit a file but not interactively. It is also a tool for processing text files.
Example of sed usage:
Get lines from 1 to 3 from a file
$cat test.txt | sed -e '1,3!d'
sed is telling - Delete all except line from 1 to 3. It is acting on just the output of cat command and not on actual file. So, nothing will happen to original file. ! is for negation. 'd' is for delete.

$sed -e '1,3d' simple.txt
Delete lines 1 to 3 from simple.txt. Again this will not change the original file.
-e tells sed to take next item as sed command.
$cat simple.txt | sed -e '1,3d'
Same as above.
What you can do with "sed"?
Sed can append lines, remove lines, change lines, rearrange lines, substitute text strings and move. Using sed you can write scripts that can become powerful text manipulation commands.
You can write sed command in a file and run:
$sed -f sed1.cmd copy.txt
-f is telling to take command from file.

Substitution using sed command (Find and replace):
$sed 's/Satya/Sathya/ test.txt > test.txt
Here I am redirecting sed output to test.txt file. So, test.txt file will be modified here. -e is not used there. (Why? I don't know).

setEnv - Set or display value of environment vars.

shutdown -
$shutdown -r minutes
for restart in given minutes.
$shutdown -h

ssh - Secure shell for remote login. This apparently has the same functionality as rlogin, telnet, ftp, ssh, etc. with better security and encryption features.

$ssh -V
tells ssh client version

scp - File transfer to/from local host
copy file from localhost to remote.

$scp localFileName.txt satya@remotehost.ex.com:/home/anyuser/localhostFile.txt
This will copy a local file named 'localFileName.txt' to remote host at specified path.

$scp satya@remotehost.ex.com:/home/anyuser/remotefile.txt remotefile.txt
Copy remotefile.txt from remote system to local system.

Debug Info:
$scp -l -v username remotehost.com

Q. What to do if you want to come back to localhost for a while and do not want to logout from remote host?
At remote host terminal type:
remotehost$ ~^Z
(You will not see whatever you entered immediately. )

$jobs // tells about running jobs.
[1]+ stopped ssh -1 uname remotehost

Bring back the background job to resume remotehost connection -
$fg %1
1 is the job number.

Get stat/ get ssh session stat:
remotehost$~s
~s will not visible when you type it.

SCP command uses the ssh command and they are much alike. Few differences:
SCP cmd can be used in three way - to copy from a (remote) server to local,
and to copy from local (your pc) to remote system, and to copy from a remote PC to another PC. In the third case, data is transferred directly between systems.

$ssh -p port user@server

Running a cmd at remote system:
$ssh user@yourMachine updatedb

$ssh youruser@system 'wall "Hey, I just remember you!"'
Single quote prevent bash from trying to interpret the command.

SSH - Secure shell
SCP - Secure copy

su - Super user login

sudo - allows individual users to have root permission to perform specified tasks. You need to be in sudoer list added by root for specified task.

symlinks - an utility to provide list of symlinks and info about symbolic links.

symlink - make a new name for a file. Deleting the name referred to by a symlink will actually delete the file (unless it also has other hard links). If this behavior is not desired, use link/ln.

tail - display the last part of the file.
$tail -10

head - display first part of the file. Head and tail helps to take a view of the file without opening the large file.

By default, head and tail commands show 10 lines. Head shows part from top and tail shows part from the bottom of the file.

$head -5 filename
// 5 lines

$tail -20 filename
// 20 lines

talk - visual communication program that copies lines from one terminal to that of another user terminal.

tar - file comparison and archiving utility. Uncompress tarball
$tar -xvzf file.tgz

Create a tarball:
$tar -cvzf file.tgz sourceFileName
SourceFile can be single file, wildcard, or the name of a dir.

tee - The program output to standard output as well as to a given file. It is primarily used with pipes and filters.

test - evaluates an expression or compares arguments.

top - dynamically display process status.

touch - update access and modification times of a file. If file does not exist, an empty file is created.

type - will help you know if a command is an alias, a built-in or an independent command.

$type echo
Output: echo is shell command
$type -t echo
Output: buildin
Possible types are alias, function, builtin, file, and keyword.

Read, write and execute permission -
Numeric value of read, write and execute permission:
Read -      4
Write -     2
Execute - 1

cat file1 - file2 - It will show output from one file and then wait for you intput then after your input is over then it prints file2 on screen. Complete your input by pressing ^d.

Continue …..

    Source:
  1. http://www.er.uqam.ca/nobel/r10735/unixcomm.html,
  2. Various.
  • # 1 - by PHP programming

    Great thinking about globalization issue . I appreciate with your blog fully,so give inform me when you have update your blog. Thanks

    • # 2 - by Satya Prakash

      no fixed time for updating it.

Comments are open for an year period. Please, write here on Facebook page.