Professional Documents
Culture Documents
Find Command Grep Command Sed Command ls Command Copy (cp) Command Cut Command Crontab Command SCP Command WC Command SSH Command Kill Command FTP (File Transfer Protocol) Command Tar Command Sort Command Zip Command Top Command Chmod Command Translate (tr) Command Uniq Command Tail Command Head Command
Kernel is the core part of Unix which interacts with the hardware for low level functions. Shell is the outer unit of Unix which interacts with the user to perform the functions. File System.
The diagram looks like any upside-down tree. The slash (/) indicates the root directory. Names like etc, usr, local are directories and science.txt is a file. The regular files in Unix are the leaves in a tree structure.
Regular files Directories Special or Device files Regular Files Regular files hold data and executable programs. Executable programs are the commands (ls) that you enter on the prompt. The data can be anything and there is no specific format enforced in the way the data is stored. The regular files can be visualized as the leaves in the UNIX tree.
Directories Directories are files that contain other files and sub-directories. Directories are used to organize the data by keeping closely related files in the same place. The directories are just like the folders in windows operating system. The kernel alone can write the directory file. When a file is added to or deleted from this directory, the kernel makes an entry. A directory file can be visualized as the branch of the UNIX tree. Special Or Device Files These files represent the physical devices. Files can also refer to computer hardware such as terminals and printers. These device files can also refer to tape and disk drives, CD-ROM players, modems, network interfaces, scanners, and any other piece of computer hardware. When a process writes to a special file, the data is sent to the physical device associated with it. Special files are not literally files, but are pointers that point to the device drivers located in the kernel. The protection applicable to files is also applicable to physical devices.
File Permission
File ownership is an important component of UNIX that provides a secure method for storing files. Every file in UNIX has the following attributes:
Owner permissions: The owner's permissions determine what actions the owner of the file can perform on the file. Group permissions: The group's permissions determine what actions a user, who is a member of the group that a file belongs to, can perform on the file. Other (world) permissions: The permissions for others indicate what action all other users can perform on the file.
File Access Modes: The permissions of a file are the first line of defense in the security of a Unix system. The basic building blocks of Unix permissions are the read, write, and execute permissions, which are described below: 1. Read: Grants the capability to read ie. view the contents of the file. 2. Write: Grants the capability to modify, or remove the content of the file. 3. Execute: User with execute permissions can run a file as a program.
Let see some practical exercises on using find command. 1. How to run the last executed find command?
!find
This will execute the last find command. It also displays the last find command executed along with the result on the terminal. 2. How to find for a file using name?
This will find all the files with name "sum.java" in the current directory and sub-directories. 3. How to find for files using name and ignoring case?
This will find all the files with name "sum.java" while ignoring the case in the current directory and sub-directories.
This will find for the file "sum.java" in the current directory only 5. How to find for files containing a specific word in its name?
It displayed all the files which have the word "java" in the filename 6. How to find for files in a specific directory?
This will look for the files in the /etc directory with "java" in the filename 7. How to find the files whose name are not "sum.java"?
This is like inverting the match. It prints all the files except the given file "sum.java".
You can see here the find command displayed all the files with name "sum.java" in the current directory and sub-directories.
./sum.java
10. How to find the largest file in the current directory and sub directories
11. How to find the smallest file in the current directory and sub directories
12. How to find files based on the file type? a. Finding socket files
find . -type s
b. Finding directories
find . -type d
find . -type f
13. How to find files based on the size? a. Finding files whose size is exactly 10M
14. How to find the files which are modified after the modification of a give file.
This will display all the files which are modified after the file "sum.java" 15. Display the files which are accessed after the modification of a give file.
16. Display the files which are changed after the modification of a give file.
This will display the files which have read, write, and execute permissions. To know the permissions of files and directories use the command "ls -l".
find . -mtime -1
20. How to find the files which are modified 30 minutes back
find . -atime -1
find . -ctime -2
!grep
This displays the last executed grep command and also prints the result set of the command on the terminal. 2. Search for a string in a file This is the basic usage of grep command. It searches for the given string in the specified file.
This searches for the string "Error" in the log file and prints all the lines that has the word "Error". 3. Searching for a string in multiple files.
This is also the basic usage of the grep command. You can manually specify the list of files you want to search or you can specify a file pattern (use regular expressions) to search for.
4. Case insensitive search The -i option enables to search for a string case insensitively in the give file. It matches the words like "UNIX", "Unix", "unix".
This will search for the lines which starts with a number. Regular expressions is huge topic and I am not covering it here. This example is just for providing the usage of regular expressions. 6. Checking for the whole words in a file. By default, grep matches the given string/pattern even if it found as a substring in a file. The -w option to grep makes it match only the whole words.
10. Searching for a sting in all files recursively You can search for a string in all the files under the current directory and sub-directories with the help -r option.
grep -r "string" *
11. Inverting the pattern match You can display the lines that are not matched with the specified search sting pattern using the -v option.
12. Displaying the non-empty lines You can remove the blank lines using the grep command.
13. Displaying the count of number of matches. We can find the number of lines that matches the given string/pattern
14. Display the file names that matches the pattern. We can just display the files that contains the given string/pattern.
grep -l "string" *
15. Display the file names that do not contain the pattern. We can display the files which do not contain the matched string/pattern.
grep -L "string" *
16. Displaying only the matched pattern. By default, grep displays the entire line which has the matched string. We can make the grep to display only the matched string by using the -o option.
17. Displaying the line numbers. We can make the grep command to display the position of the line which contains the matched string in a file using the -n option
18. Displaying the position of the matched string in the line The -b option allows the grep command to display the character position of the matched string in a file.
19. Matching the lines that start with a string The ^ regular expression pattern specifies the start of a line. This can be used in grep to match the lines which start with the given string or pattern.
20. Matching the lines that end with a string The $ regular expression pattern specifies the end of a line. This can be used in grep to match the lines which end with the given string or pattern.
>cat file.txt unix is great os. unix is opensource. unix is free os. learn operating system. unixlinux which one you choose.
1. Replacing or substituting string Sed command is mostly used to replace the text in a file. The below simple sed command replaces the word "unix" with "linux" in the file.
>sed 's/unix/linux/' file.txt linux is great os. unix is opensource. unix is free os. learn operating system. linuxlinux which one you choose.
Here the "s" specifies the substitution operation. The "/" are delimiters. The "unix" is the search pattern and the "linux" is the replacement string. By default, the sed command replaces the first occurrence of the pattern in each line and it won't replace the second, third...occurrence in the line.
2. Replacing the nth occurrence of a pattern in a line. Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line. The below command replaces the second occurrence of the word "unix" with "linux" in a line.
>sed 's/unix/linux/2' file.txt unix is great os. linux is opensource. unix is free os. learn operating system. unixlinux which one you choose.
3. Replacing all the occurrence of the pattern in a line. The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line.
>sed 's/unix/linux/g' file.txt linux is great os. linux is opensource. linux is free os. learn operating system. linuxlinux which one you choose.
4. Replacing from nth occurrence to all occurrences in a line. Use the combination of /1, /2 etc and /g to replace all the patterns from the nth occurrence of a pattern in a line. The following sed command replaces the third, fourth, fifth... "unix" word with "linux" word in a line.
>sed 's/unix/linux/3g' file.txt unix is great os. unix is opensource. linux is free os. learn operating system. unixlinux which one you choose.
5. Changing the slash (/) delimiter You can use any delimiter other than the slash. As an example if you want to change the web url to another url as
In this case the url consists the delimiter character which we used. In that case you have to escape the slash with backslash character, otherwise the substitution won't work. Using too many backslashes makes the sed command look awkward. In this case we can change the delimiter to another character as shown in the below example.
6. Using & as the matched string There might be cases where you want to search for the pattern and replace that pattern by adding some extra characters to it. In such cases & comes in handy. The & represents the matched string.
>sed 's/unix/{&}/' file.txt {unix} is great os. unix is opensource. unix is free os. learn operating system. {unix}linux which one you choose.
>sed 's/unix/{&&}/' file.txt {unixunix} is great os. unix is opensource. unix is free os. learn operating system. {unixunix}linux which one you choose.
7. Using \1,\2 and so on to \9 The first pair of parenthesis specified in the pattern represents the \1, the second represents the \2 and so on. The \1,\2 can be used in the replacement string to make changes to the source string. As an example, if you want to replace the word "unix" in a line with twice as the word like "unixunix" use the sed command as below.
>sed 's/\(unix\)/\1\1/' file.txt unixunix is great os. unix is opensource. unix is free os. learn operating system. unixunixlinux which one you choose.
The parenthesis needs to be escaped with the backslash character. Another example is if you want to switch the words "unixlinux" as "linuxunix", the sed command is
>sed 's/\(unix\)\(linux\)/\2\1/' file.txt unix is great os. unix is opensource. unix is free os. learn operating system. linuxunix which one you choose.
>sed 's/^\(.\)\(.\)\(.\)/\3\2\1/' file.txt inux is great os. unix is opensource. unix is free os. aelrn operating system. inuxlinux which one you choose.
The /p print flag prints the replaced line twice on the terminal. If a line does not have the search pattern and is not replaced, then the /p prints that line only once.
>sed 's/unix/linux/p' file.txt linux is great os. unix is opensource. unix is free os. linux is great os. unix is opensource. unix is free os. learn operating system. linuxlinux which one you choose. linuxlinux which one you choose.
9. Printing only the replaced lines Use the -n option along with the /p print flag to display only the replaced lines. Here the -n option suppresses the duplicate rows generated by the /p flag and prints the replaced lines only one time.
>sed -n 's/unix/linux/p' file.txt linux is great os. unix is opensource. unix is free os. linuxlinux which one you choose.
If you use -n alone without /p, then the sed does not print anything.
10. Running multiple sed commands. You can run multiple sed commands by piping the output of one sed command as input to another
sed command.
>sed 's/unix/linux/' file.txt| sed 's/os/system/' linux is great system. unix is opensource. unix is free os. learn operating system. linuxlinux which one you chosysteme.
Sed provides -e option to run multiple sed commands in a single sed command. The above output can be achieved in a single sed command as shown below.
>sed -e 's/unix/linux/' -e 's/os/system/' file.txt linux is great system. unix is opensource. unix is free os. learn operating system. linuxlinux which one you chosysteme.
11. Replacing string on a specific line number. You can restrict the sed command to replace the string on a specific line number. An example is
>sed '3 s/unix/linux/' file.txt unix is great os. unix is opensource. unix is free os. learn operating system. linuxlinux which one you choose.
The above sed command replaces the string only on the third line.
You can specify a pattern to the sed command to match in a line. If the pattern match occurs, then only the sed command looks for the string to be replaced and if it finds, then the sed command replaces the string.
>sed '/linux/ s/unix/centos/' file.txt unix is great os. unix is opensource. unix is free os. learn operating system.
Here the sed command first looks for the lines which has the pattern "linux" and then replaces the word "unix" with "centos". 14. Deleting lines. You can delete the lines a file by specifying the line number or a range or numbers.
15. Duplicating lines You can make the sed command to print each line of a file two times.
16. Sed as grep command You can make sed command to work as similar to grep command.
Here the sed command looks for the pattern "unix" in each line of a file and prints those lines that
has the pattern. You can also make the sed command to work as grep -v, just by using the reversing the sed with NOT (!).
The ! here inverts the pattern match. 17. Add a line after a match. The sed command can add a new line after a pattern match is found. The "a" command to sed tells it to add a new line after a match is found.
>sed '/unix/ a "Add a new line"' file.txt unix is great os. unix is opensource. unix is free os. "Add a new line" learn operating system. unixlinux which one you choose. "Add a new line"
18. Add a line before a match The sed command can add a new line before a pattern match is found. The "i" command to sed tells it to add a new line before a match is found.
>sed '/unix/ i "Add a new line"' file.txt "Add a new line" unix is great os. unix is opensource. unix is free os.
learn operating system. "Add a new line" unixlinux which one you choose.
19. Change a line The sed command can be used to replace an entire line with a new line. The "c" command to sed tells it to change the line.
>sed '/unix/ c "Change line"' file.txt "Change line" learn operating system. "Change line"
20. Transform like tr command The sed command can be used to convert the lower case letters to upper case letters by using the transform "y" option.
>sed 'y/ul/UL/' file.txt Unix is great os. Unix is opensoUrce. Unix is free os. Learn operating system. UnixLinUx which one yoU choose.
Here the sed command transforms the alphabets "ul" into their uppercase format "UL"
Sed Command to Delete Lines: Sed command can be used to delete or remove specific lines which matches a given pattern or in a particular position in a file. Here we will see how to delete lines using sed command with various examples. The following file contains a sample data which is used as input file in all the examples:
Here N indicates Nth line in a file. In the following example, the sed command removes the first line in a file.
debian ubuntu
2. Delete last line or footer line or trailer line The following sed command is used to remove the footer line in a file. The $ indicates the last line of a file.
3. Delete particular line This is similar to the first example. The below sed command removes the second line in a file.
4. Delete range of lines The sed command can be used to delete a range of lines. The syntax is shown below:
Here m and n are min and max line numbers. The sed command removes the lines from m to n in the file. The following sed command deletes the lines ranging from 2 to 4:
5. Delete lines other than the first line or header line Use the negation (!) operator with d option in sed command. The following sed command removes all the lines except the header line.
fedora debian
Here the sed command removes lines other than 2nd, 3rd and 4th. 8. Delete first and last line You can specify the list of lines you want to remove in sed command with semicolon as a delimiter.
The ^$ indicates sed command to delete empty lines. However, this sed do not remove the lines that contain spaces.
^ is to specify the starting of the line. Above sed command removes all the lines that start with character 'u'. 11. Delete lines that end with specified character
$ is to indicate the end of the line. The above command deletes all the lines that end with character 'x'. 12. Delete lines which are in upper case or capital letters
14. Delete lines starting from a pattern till the last line
Here the sed command removes the line that matches the pattern fedora and also deletes all the lines to the end of the file which appear next to this matching line. 15. Delete last line only if it contains the pattern
Here $ indicates the last line. If you want to delete Nth line only if it contains a pattern, then in place of $ place the line number. Note: In all the above examples, the sed command prints the contents of the file on the unix or linux terminal by removing the lines. However the sed command does not remove the lines from the source file. To Remove the lines from the source file itself, use the -i option with sed command.
If you dont wish to delete the lines from the original source file you can redirect the output of the sed command to another file.
The syntax of sed command to print lines using line addresses is:
1. Display lines matching a pattern The following examples prints the lines that contain the pattern virtual using sed and grep commands.
ls [options] [pathnames]
1. Write a unix/linux ls command to display the hidden files and directories? To display the hidden files and directories in the current directory use the -a option of the ls command.
Hidden files are the one whose name starts with dot (.). The las -a displays the current directory (.) and parent directory (..) also. If you want to exclude the current directory, parent directory, then use -A option.
2. Write a unix/linux ls command to classify the files with special characters The -F option to ls command classifies the files. It marks the
Directories with trailing slash (/) Executable files with trailing asterisk (*) FIFOs with trailing vertical bar (|) Symbolic links with trailing at the rate sign (@) Regular files with nothing
3. Write a unix/linux ls command to print each file in a separate line? The -1 option to the ls command specifies that each file should be displayed on a separate line
4. Write a unix/linux ls command to display the inode number of file? In some cases, you want to know the inode number of a file. Use -i option to the ls command to print the inode number of a file.
5. Write a unix/linux ls command to display complete information about the files? The -l option provides lots of information about the file type, owner, group, permissions, file size, last modification date.
> ls -l total 16 drwxr-xr-x 2 matt db 4096 Jan 30 23:08 documents -rw-r--r-- 1 matt db 49 Jan 31 01:17 sum.pl
The first character indicates the type of the file. - for normal file, d for directory, l for link file and s for socket file The next 9 characters in the first field represent the permissions. Each 3 characters refers the read (r), write (w), execute (x) permissions on owner, group and others. - means no permission. The second field indicates the number of links to that file. The third field indicates the owner name. The fourth field indicates the group name. The fifth field represents the file size in bytes. The sixth field represents the last modification date and time of the file. And finally the seventh field is the name of the file. 6. Write a unix/linux ls command to sort the files by their modification time? The -t option allows the ls command to sort the files in descending order based on the modification time.
7. Write a unix/linux ls command to sort the files in ascending order of modification time? The -r option reverses the order of the files displayed. Combine the -t and -r options to sort the files in ascending order.
8. Write a unix/linux ls command to print the files recursively? So far the ls command prints the files in the current directory. Use the -R option to recursively print the files in the sub-directories also.
9. Write a unix/linux ls command to print the files in a specific directory? You can pass a directory to the ls command as an argument to print for the files in it.
> ls /usr/local/bin
> ls -x
Examples of cp Command 1. Write a unix/linux cp command to copy file in to a directory? The basic usage of cp command is to copy a file from the current directory to another directory.
cp sum.pl tmp/
The cp command copies the file sum.pl into the tmp directory. The cp command does not remove the source file. It just copies the file into a new location. If a file with the same name as the source exists in the destination location, then by default the cp command overwrites that new file 2. Write a unix/linux cp to prompt for user before overwriting a file ( Interactive cp command)? The -i option to the cp command provides the ability to prompt for a user input whether to overwrite the destination file or not.
If you enter y, then the cp command overwrites the destination file, otherwise the cp command does not copy the file.
3. Write a unix/linux cp command to copy multiple files in to a new directory? You can specify multiple files as the source and can copy to the new location.
The cp command copies the log.dat, bad.dat files in the current directory to the tmp directory.
4. Write a unix/linux cp command to do a Regular expression copy? You can copy a set of files by specifying a regular expression pattern.
cp *.dat tmp/
Here the cp command copies all the files which has "dat" as suffix to the destination directory. 5. Write a unix/linux cp command to copy a file in to the current directory? You can copy a file from a different directory to the current directory.
cp /usr/local/bin/multiply.sh .
Here the cp command copies the multiply.sh file in the /usr/local/bin directory the current directory. The dot (.) indicates the current directory. 6. Write a unix/linux cp command to copy all the files in a directory? The cp command can be used to copy all the files in directory to another directory.
cp docs/* tmp/
This command copies all the files in the docs directory to the tmp directory. 7. Write a unix/linux cp command to copy files from multiple directories? You can copy the files from different directories into a new location.
The command copies the files from docs and script directories to the destination directory tmp.
8. Write a unix/linux cp command to Copy a directory. You can recursively copy a complete directory and its sub directory to another location using the cp command
cp -r docs tmp/
This copies the complete directory docs into the new directory tmp 9. Write a unix/linux cp command to Forcibly copy a file with -f option? You can force the cp command to copy an existing destination file even it cannot be opened.
cp -f force_file.txt /var/tmp/
1. Write a unix/linux cut command to print characters by position? The cut command can be used to print characters in a line by specifying the position of the characters. To print the characters in a line, use the -c option in cut command
The above cut command prints the fourth character in each line of the file. You can print more than one character at a time by specifying the character positions in a comma separated list as shown in the below example
This command prints the fourth and sixth character in each line.
2.Write a unix/linux cut command to print characters by range? You can print a range of characters in a line by specifying the start and end position of the characters.
linu
The above cut command prints the characters from fourth position to the seventh position in each line. To print the first six characters in a line, omit the start position and specify only the end position.
To print the characters from tenth position to the end, specify only the start position and omit the end position.
If you omit the start and end positions, then the cut command prints the entire line.
3.Write a unix/linux cut command to print the fields using the delimiter? You can use the cut command just as awk command to extract the fields in a file using a delimiter. The -d option in cut command can be used to specify the delimiter and -f option is used to specify the field position.
This command prints the second field in each line by treating the space as delimiter. You can print more than one field by specifying the position of the fields in a comma delimited list.
The above command prints the second and third field in each line. Note: If the delimiter you specified is not exists in the line, then the cut command prints the entire line. To suppress these lines use the -s option in cut command. 4. Write a unix/linux cut command to display range of fields? You can print a range of fields by specifying the start and end position.
The above command prints the first, second and third fields. To print the first three fields, you can ignore the start position and specify only the end position.
To print the fields from second fields to last field, you can omit the last field position.
5. Write a unix/linux cut command to display the first field from /etc/passwd file? The /etc/passwd is a delimited file and the delimiter is a colon (:). The cut command to display the first field in /etc/passwd file is
Using the cut command extract the portion after the dot. First reverse the text in each line and then apply the command on it.
* * * * * command to be executed
MI HH
: Minutes : Hours
from 0 to 59 from 0 to 23
DOM : Day of month from 0 to 31 MON : Months DOW : Day of week from 1 to 12 from 0 to 7 (0 or 7 represents Sunday)
Let see the usage of crontab command with examples. 1. List crontab entries You can list out all the jobs which are already scheduled in cron. Use "crontab -l" for listing the jobs.
crontab -l
0 0 * * *
/usr/local/bin/list_unix_versions.sh
The above contab command displays the cron entries. Here the shell script for listing the unix versions (list_unix_version.sh) is scheduled to run daily at midnight. 2. List crontab entries of other users To list the corntab entries of other user in the unix, use the -u option with crontab. The syntax is shown below:
crontab -u username -l
3. Removing all crontab entries You can un-schedule all the jobs by removing them from the crontab. The syntax for removing all the crontab entries is
crontab -r
4. Editing the crontab You can edit the crontab and add a new job to it. You can also remove an existing job from the crontab. Use the -e option for editing the crontab.
crontab -e
This will open a file in VI editor. Now use the VI commands for adding, removing the jobs and for saving the crontab entries. 5. Schedule a job to take oracle backup on every Sunday at midnight Edit crontab using "crontab -e" and append the following entry in the file.
0 0 * * 0 /usr/local/bin/oracle_backup.sh
6. Schedule a job to run every six hours in a day You can schedule a job to run more than once in a day. As an example the following crontab entry takes the mysql backup more than once in a day.
0 0,6,12,18 * * * /usr/bin/mysql_backup.sh
Here the list 0,6,12,18 indicates midnight, 6am, 12pm and 6pm respectively.
7. Schedule job to run for the first 15 days of the month. You can schedule a job by specifying the range of values for a field. The following example takes the sql server backup daily at midnight for the first 15 days in a month.
0 0 * 1-15 * /usr/bin/sql_server_backup.sh
8. Schedule job to run every minute. The following crontab command runs the command to send emails to group of users for every
minute.
* * * * * /bin/batch_email_send.sh
9. Taking backup of cron entries Before editing the cron entries, it is good to take backup of the cron entries. So that even if you do mistake you can get back those entries from the backup.
10. Restoring the cron entries You can restore the cron entries from the backup as
crontab cron_backup.dat
Note: you cannot schedule a job to run at seconds level as the minimum allowed scheduling is at minute level.
Copies files within in the same machine Copies files from local machine to remote machine. Copies files from remote machine to local machine. Copies files between two different remote servers.
User is the one who have the permissions to access the files and directories. User should have read permissions if it is a source and write permissions if it is the destination. From_Host: hostname or Ip address where the source file or directory resides. This is optional if the from host is the host where you are running the scp command. Source_File: Files or directories to be copied to the destination. To_Host: Destination host where you want to copy the files. You can omit this when you want to copy the files to the host where you are issuing the scp command. Destination_File: Name of the file or directory in the target host. SCP Command Options: The important SCP command options are listed below:
-r : Recursively copies the contents of source files or directories. -p : Preserves the access time, modification time, permissions of the source files in the destination. -q : Progress bar in not displayed -v : verbose mode. Displays debugging messages. -P : copy files using the specified port number.
SCP Command Examples: Let see the examples of scp command in unix or linux system. 1. Copying with in the same system You can use the scp command just like the cp command to copy files from one directory to another directory.
This command copies the file unix-storage.dat from current directory to the /var/tmp directory. 2. Copy file from local host to remote server This is most frequently used operation to transfer files in unix system.
This command connects to the remote host and copies the specified file to the /remote/directory/. 3. Copy files from remote host to local server. This operation is used when taking backup of the files in remote server.
scp user@remotehost:/usr/backup/oracle_backup.dat .
This command copies the oracle backup file in the remote host to the current directory.
4. Copying files between two remote servers The scp command can also be used to copy files between two remote hosts.
The above command copies the mysql bakup shell script from the source remote host the /var/tmp directory of target remote host. 5. Copying a directory.
To copy all the files in a directory, use the -r option with the scp command. This makes the scp command to copy the directory recursively.
wc [options] filenames
-l : Prints the number of lines in a file. -w : prints the number of words in a file. -c : Displays the count of bytes in a file. -m : prints the count of characters from a file. -L : prints only the length of the longest line in a file.
Let see how to use the wc command with few examples. Create the following file in your unix or linux operating system.
> cat unix_wc.bat Oracle Storage unix distributed system linux file server debian server Oracle backup server
WC Command Examples: 1. Printing count of lines This is the most commonly used operation to find the number of lines from a file. Run the below command to display the number of lines:
wc -l unix_wc.bat
Here in the output, the first field indicates count and second field is the filename 2. Displaying the number of words.
Just use the -w option to find the count of words in a file. This is shown below:
wc -w unix_wc.bat
3. Print count of bytes, count of characters from a file We can use the -c and -m options to find the number of bytes and characters respectively in a file.
4. Print the length of longest line The -L option is used to print the number of characters in the longest line from a file.
wc -L unix_wc.bat
5. Print count of lines, words and characters. If you dont specify any option to the wc command, by default it prints the count of lines, words and characters. This is shown below:
wc unix_wc.bat
Let see the examples of ssh command. SSH Command Examples: 1. Logging to a remote server You can login to a remote server from the local host as shown below:
Alternatively you can use the below ssh command for connecting to remote host:
Note: If you are logging for the first time, then it will prints a message that host key not found and you can give yes to continue. The host key of the remote server will be cached and added to the .ssh2/hostkeys directory in your home directory. From second time onwards you just need to enter the password. 2. Logging out from remote server Simply enter the exit command on the terminal to close the connection. This is shown below:
3. Running remote commands from local host Sometimes it is necessary to run the unix commands on the remote server from the local host. An example is shown below:
The ssh command connects to the remote host, runs the ls command, prints the output on the local host terminal and exits the connection from remote host. Let see whether the ls command actually displayed the correct result or not by connecting to the remote host.
4. Version of the SSH command We can find the version of SSH installed on the unix system using the -V option to the ssh. This is shown below:
We can use the scp command to copy the files securely between the local host and remote host using the ssh authentication. To copy the file from local host to remote hosts /var/tmp/ directory, run the below scp command.
To copy the file from remote hosts /usr/local/bin/ directory to local hosts current directory, run the below scp command.
scp user@remote-host:/usr/local/bin/add.sh .
kill -l
pid : list of process that kill command should send a signal -s signal : send the specified signal to the process -l : list all the available signals.
Let see some of the useful kill command examples in unix or linux system. Kill Command Examples: 1. Listing all the signal names. Run the kill command with -l option to list all the available signal names.
> kill -l HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM USR1 USR2 CLD PWR WINCH URG POLL STOP TSTP CONT TTIN TTOU VTALRM PROF XCPU XFSZ WAITING LWP FREEZE THAW CANCEL LOST RTMIN RTMIN+1 RTMIN+2 RTMIN+3 RTMAX-3 RTMAX-2 RTMAX-1 RTMAX
Some the important signals which we use in our daily usage ar listed below:
Description Used to check access to the process id Hup signal. Terminates the process. Interrupt signal. Terminating the process
3 9 24 26
Quit signal. Terminate process with core dump Forcibly killing a process Pausing the process Runs a stopped process
To know more about a signal, check in man pages. To know about the signal 9, run the below man command:
man 5 signal
2. Getting the process id To know the process id of a process running in the unix system, use the ps command as
The second field in the output is the process Id. Here the /usr/local/sbin/sshd -R is running with the process id 4529. 3. Killing a process. To kill processes simply pass the process id to the kill command. This is shown below:
kill 4529
Use the -9 option with the kill command to kill a process force fully. The following kill command terminates the process forcefully:
kill -9 1567 kill -SIGKILL 1567 kill -KILL 1567 kill -s SIGKILL 1567 kill -s KILL 1567
FTP Command Examples: If you are using windows operating system, open the command prompt and practice the below FTP commands. If you are using unix or linux operating systems, just simply type the ftp command on the terminal. 1. Connecting to Remote Host First you need to connect to a remote host before doing any operations. You can use any one of the following methods to connect to a remote host. First method is
Once the ftp connects to the remote server name, it will prompt you to enter the user name and password. After successful login, your terminal or prompt changes to "ftp>". Another method is to use the open option with ftp command. This is shown below:
If the ftp command fails to connect to the remote server, then you will get the below error:
2. Copy file from remote machine to local machine. The get option is used to download or transfer a file from the remote system to the local system.
This will download the specified file (windows-cleveland.bat) from the remote systems current directory. 3. Copying multiple files from remote machine to local machine. You can use the mget to transfer multiple files from the remote host to local host.
ftp>mget *.png
This will download all the png images to the local machine. 4. Transferring file from local server to remote server The put option is used to copy the file from the local host to the remote host.
ftp>put linux-virtual-server.rpm
This command puts the rpm file into the remote machine. 6. Executing commands in remote machine. After connecting to the remote network machine using the ftp, you can run commands like ls to list the files, cd to change directory and many more.
ftp> ls
This will list the files and directories in the remote machines current directory.
7. Executing commands in local machine. Once you have connected to the remote host, to run the commands on local machine you need to exit from the ftp connection. Instead of this, there is a way to run commands on local host without exiting from the ftp connection. Use the ! symbol before the command you want to run.
ftp> !ls
Now this will list the files in the local machines current directory. 9. Deleting files on remote machine You can use the delete or mdelete to remove a single file or multiple files in the remote machine.
ftp>quit