The document provides examples for various Linux commands used to manage files, directories, processes and permissions. It covers commands like ls, tar, gzip, grep, find, sed, awk, vim, sort, pwd, crontab, ps, df, kill, rm, cp, mv, cat, chmod, mkdir, rmdir, wc and echo along with examples of their usage. It also includes examples to display the first/last few lines of a file, show non-printable characters, check the status of the last command and connect to an Oracle database from a shell script.
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
23 views
Unix Commants Interview
The document provides examples for various Linux commands used to manage files, directories, processes and permissions. It covers commands like ls, tar, gzip, grep, find, sed, awk, vim, sort, pwd, crontab, ps, df, kill, rm, cp, mv, cat, chmod, mkdir, rmdir, wc and echo along with examples of their usage. It also includes examples to display the first/last few lines of a file, show non-printable characters, check the status of the last command and connect to an Oracle database from a shell script.
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2
1)How to list down the files and directories
$ ls:Olny list not in long list format
$ ls –ltr:To display in long list format 2)Display Hidden Files $ ls -a 3)tar command examples compress multiple files $ tar cvf archive_name.tar dirname/ Extract from an existing tar archive. $ tar xvf archive_name.tar 4)gzip command examples compress a single file $ gzip test.txt To uncompress a *.gz file : $ gzip -d test.txt.gz 5)grep command examples Search for a given string in a file (case in-sensitive search) : $ grep -i "the" demo_file Print the matched line, along with the 3 lines after it.: $ grep -A 3 -i "example" demo_text 6)find command examples Find files using file-name ( case in-sensitve find):# find -iname "MyCProgram.c" 7)sed command examples Mainly we use SED cmd to find replace in a file or used for modifying the files in unix Replacing or substituting string Sed command is mostly used to replace the text in a file. $>sed 's/unix/linux/' file.txt $>sed –i '5,7 d' file.txt 8)Remove duplicate lines using awk $ awk '!($0 in array) { array[$0]; print }' temp 9)vim command examples Get from nth line onwards file content : $ vim +5 filename.txt The above command will display 5th line onwards the data from filename.txt 10)sort command examples Sort a file in ascending order $ sort psk.txt Sort a file in descending order $ sort -r psk.txt 11)pwd command :pwd is Print working directory. $ pwd 12)crontab command examples Crontab command is used to schedule jobs. You must have permission to run this command by Unix Administrator. Jobs are scheduled in five numbers, as follows.) 13)ps command examples To view current running processes. $ ps -ef | more 14)df command examples Displays the file system disk space usage. By default df -k displays output in bytes $ df -k df -h displays output in human readable form. i.e size will be displayed in GB’s. 15)kill command examples Use kill command to terminate a process. First get the process id using ps -ef command, then use kill -9 to kill the running Linux process as shown below. You can also use killall, pkill, xkill to 16)rm command examples Get confirmation before removing the file. $ rm -i filename.txt 17)cp command examples Copy file1 to file2 preserving the mode, ownership and timestamp.$ cp -p file1 file2 18)mv command examples Rename file1 to file2. $ mv file1 file2 19)cat command examples Following example prints the content of file1followed by file2 to stdout. $ cat file1 file2 20)chmod command examples chmod command is used to change the permissions for a file or directory. Give full access to user and group (i.e read, write and execute ) on a specific file. $ chmodug+rwx file.txt 21)mkdir command examples How to create a directory :$ mkdir temp 22)rmdir - remove a directory To remove the Dircetories :$ rmdir info 23)wc - display a count of lines, words and characters wc counts the number of lines, words, and characters in files. The command gives no of lines in a file$wc-l psk.txt 24)echo command echo is used to print the message echo “PSK training Istitute” > PSK.txt How to print/display first n lines of a file? There are many ways to do this. However the easiest way to display the first line of a file is using the [head] command. Below command will display first 5 lines from the file $> head -5 file.txt How to print/display the last n lines of a file? Below command will display last 5 lines $> tail -10 file.txt How to display n-th line of a file? $>sed –n '<n> p' file.txt You need to replace <n> with the actual line number. So if you want to print the 4th line, $>sed –n '4 p' test How to show the non-printable characters in a file? Open the file in VI editor. Go to VI command mode by pressing [Escape] and then [:]. Then type [set list]. This will show you all the non-printable characters, e.g. Ctrl-M characters (^M) etc., in the file. How to check if the last command was successful in Unix? $? Will gives the status of previous command. To display the value assigned to shell variable $>echo $HomeOutput: /home/useri3 How to connect to the oracle database from within shell script? $ res = `sqlplus –s username/password@database_name<<EOF`