Tuesday, 29 May 2012

Unix commands reference



files and directories

 


print,clear and making directories
pwd : Display the current directoryclear :Clears the screenmkdir temp :Create a directorymkdir -p temp/junk :Create nested directories


Change directory

cd bob
cd .. (parent directory)cd - (previous directory)cd (home directory)cd ~bill (home directory of user bill):: Changing directories

copy command



cp source.txt dest.txt : Copy a file to anothercp file1.txt file2.txt destination_dir :Copy files to a directory


cp -r source_dir dest_dir
rsync -a source_dir/ dest_dir/:
Copy directories recursively

Link move,remove commands:
ln -s linked_file link :Create a symbolic link

mv source_file dest_file ::Rename a file, link or directory

rm file1.txt file2.txt ::Remove files or links
rmdir ron :Remove empty directoriesrm -rf ron :Remove non-empty directories

Listing files

ls :List all files (not starting with .) in the current directoryls -l :Display a long listingls -a :List all the files in the current directory,including ones (starting with .)ls -t :List by time (most recent files first)ls -S :List by size (biggest files first)ls -r :List with a reverse sort orderls -ltr :Long list with most recent files last


Displaying file contents

 
cat file1:Displays contents of a filecat file1 file2:Concatenate and display file contents:more file1
(or)
less file1:
Displays the contents of a file (stopping at each page)more file1 file2

(Or)
less file1 file2
  :Display the contents of several files (stopping at each page)head -10 file :Display the first 10 lines of a filetail -10 file :Display the last 10 lines of a file

Displaying startup files
cat /proc/cpuinfo :view CPU information

cat /proc/meminfo :view RAM information

cat /proc/dma :view IRQ, DMA, and I/O data

cat /proc/modules :lists devices inserted into kernel as modules

dmesg | tail
:displays hardware detected at last boot

tail /var/log/boot.log
:view system processes successful or not


File name pattern matching
cat *:Concatenate all filescat .*:Concatenate all filescat *.log:Concatenate all files ending with .logls *read*: List files with read in their namels *.?:List all files ending with . and asingle character



Handling file contents
grep ron readme.txt :Show only the lines in the file readme.txt containing the string 'ron'grep -i ron readme.txt :Case insensitive searchgrep -v ron readme.txt :Showing all the lines but the ones containing a substring:grep -r substring dir :Search through all the files in a directory

sort readme.txt :Sort lines in a given file:sort -u readme.txt (unique):Sort lines, only display duplicate ones once


Hard Drives

had :primary master IDE hdb :primary slave IDEhdc :secondary master IDEhdd :secondary slave IDE

sda :first SCSI hard disksdb :second SCSI hard diskhda1, hda2, hda3, and hda4:four primary partitions of primary master drivehda5:first logical drive of primary master drive
Changing file access rights


chmod 777 readme.txt :Add all permissions to a file
chmod 700 readme.txt
:Give owner full permissions, all else no permissionschmod 755 readme.txt :Give owner full permissions, all else read and write onlychmod 555 readme.txt :Give a file read and execute permissions for all

chmod u+w readme.txt
:Add write permissions to the current userchmod g+r readme.txt :Add read permissions to users in the file groupchmod o+x readme.txt :Add execute permissions to other userschmod a+rw readme.txt :Add read + write permissions to all users
chmod a+rX *:
Make executable files executable by all
chmod -R a+rX dir :(recursive)Make the whole directory and its contents
accessible by all users


Comparing files and directories


 

diff readme.txt readme.bak :Comparing 2 filesgvimdiff file1 file2 :Comparing 2 files (graphical)tkdiff file1 file2
kompare file1 file2

diff -r dir1 dir2: Comparing 2 directories

Looking for files


 

find . -name "*log*" : Find all files in the current (.) directory and its subdirectories with log in their namefind . -name "*.pdf" exec xpdf {} ';' : Find all the .pdf files in dir and subdirectories and run a command on eachlocate "*pub*" :Quick system-wide file search by pattern
(caution: index based, misses new files)


Redirecting command output


 

ls *.png > image_files :Redirect command output to a filels *.jpg >> image_files :Append command output to an existing filecat *.log | grep error :Redirect command output to the input of
another command

Job control


 

ps -ef :Show all running processes:top : Live hit-parade of processes (press P, M, T: sort by Processor, Memory or Time usage)kill 543 (number found in ps output):Send a termination signal to process number 543:kill -9 543:Have the kernel kill process 543kill -9 -1:Kill all processes (at least all user ones)xkill (click on the program window to kill)::Kill a graphical application


File and partition sizes

du -sh dir1 dir2 file1 file2: Show the total size on disk of files ordirectories (disk usage)wc file (word count):Number of bytes, words and lines in filedf -h . : Show the size, total space and free space of the
current partition:
df –h:Display these info for all partitions


Compressing

 


gzip readme.txt (.gz format)bzip2 readme.txt (.bz2 format, better): Compress a file called readme.txt

gunzip readme.txt.gz
bunzip2 readme.txt.bz2 :
Uncompress a file

Archiving


tar zcvf archive.tar.gz dir/:Create a compressed archive (tape archive)tar jcvf archive.tar.bz2 dir/ (better)


tar ztvf archive.tar.gz
tar jtvf archive.tar.bz2 :
Test (list) a compressed archivetar zxvf archive.tar.gz
tar jxvf archive.tar.bz2 :
Extract the contents of a compressed archive

tar options:
c: create
t: test
x: extract
j: on the fly bzip2 (un)compression
z: on the fly gzip (un)compression

Using 7-zip: (better compression than bzip2!)
7z a archive.7z <files> (add: create)7z l archive.7z (list)7z x archive.7z (extract)
7-zip compressed tar archive
(keeps user and group information)
tar cf - dir | 7z a -si dir.tar.7z (create)7z x -so dir.tar.7z | tar xf - (extract)

Handling zip archives
zip -r archive.zip <files> (create)unzip -t archive.zip (test / list)unzip archive.zip (extract)


Printing


lpr Pqueue f1.ps f2.txt (local printer): Send -PostScript or text files to queuelpq -Pqueue :List all the print jobs in queuecancel 123 queue : Cancel a print job number in queuepdf2ps doc.pdf :Print a PDF filelpr doc.ps

ps2pdf doc.ps
xpdf doc.pdf
: View a PostScript file


User management


who :List users logged on the systemwhoami :Show which user I am logged asgroups user :Show which groups user belongs tofinger user :Tell more information about usersu - ron :Switch to user ron:su - (switch user):Switch to super user (root)su (keep same directory and environment)

 


Time management

sleep 60 :Wait for 60 seconds:date :Show the current date:time command :Count the time taken by a command:

Command help
 

grep --help : Basic help (works for most commands):man grep : Access the full manual page of a command:


Misc commands


 


bc -l : Basic command-line calculator



Basic system administration



chown -R newuser:newgroup dir ::Change the owner and group of a directory and
all its contents:
shutdown -r +5:Reboot the machine in 5 minutes:shutdown -h now : Shutdown the machine now:ifconfig -a : Display all available network interfaces:ifconfig eth0 207.46.130.108:Assign an IP address to a network interfaceifconfig eth0 down :Bring down a network interfaceroute add default gw 192.168.0.1:Define a default gateway for packets to
machines outside the local network
route del default : Delete the default route:ping 207.46.130.108: Test networking with another machine:fdisk /dev/hda1:Create or remove partitions on the first IDE
hard disk:
mkfs.ext3 /dev/hda1:Create (format) an ext3 filesystem:mkfs.vfat -v -F 32 /dev/hda2:Create (format) a FAT32 filesystem:

mount /dev/uba1 /mnt/usbdisk :Mount a formatted partitionmount -o loop initrd.img /mnt/initrd: Mount a filesystem image (loopback device):umount /mnt/usbdisk: Unmount a filesystem: uname -a
uname –r:
Check the system kernel versions

 
Simple VI commands
vi : Start vivi readme.txt : Start vi with the file readme.txt

press the i key : change into insert mode

press the escape key : change into the command modeq! : quit without saving

wq or ZZ: quit and save w :save file

w readme.txt :save and give filename


No comments:

Post a Comment

Note: only a member of this blog may post a comment.