Вы находитесь на странице: 1из 5

WC command

wc -l : Prints the number of lines in a file.


wc -w : prints the number of words in a file.
wc -c : Displays the count of bytes in a file.
wc -m : prints the count of characters from a file.
wc -L : prints only the length of the longest line in a file.

Example:
[root@tecmint ~]# cat tecmint.txt

Red Hat
CentOS
Fedora
Debian
Scientific Linux
OpenSuse
Ubuntu
Xubuntu
Linux Mint
Pearl Linux
Slackware
Mandriva

Que:

How to replace the n-th line in a file with a new line in Unix?

This can be done in two steps. The first step is to remove the n-th line. And the second step is to insert a

new line in n-th line position. Here we go.

Step 1: remove the n-th line

$>sed -i'' '10 d' file.txt # d stands for delete

Step 2: insert a new line at n-th line position

$>sed -i'' '10 i This is the new line' file.txt # i stands for insert

How to check if a file is present in a particular directory in Unix?

Using command, we can do it in many ways. Based on what we have learnt so far, we can make use of [ls]

and [$?] command to do this. See below:

$> ls –l file.txt; echo $?


How to check if the last command was successful in Unix?

To check the status of last executed command in UNIX, you can check the value of an inbuilt bash variable

[$?]. See the below example:

$> echo $?

UNIX / Linux: 7 Practical PS Command Examples for Process Monitoring

1. List Currently Running Processes (ps -ef, ps -aux)

Its a commonly used example with a ps command to list down all the process which are currently
running in a machine. The following example shows the options of ps command to get all the
processes.

$ ps -ef
root 26551 5 0 Feb10 ? 00:03:41 [pdflush]
root 26570 5 0 Feb10 ? 00:00:20 [pdflush]
root 30344 3382 0 Feb21 ? 00:00:11 sshd: root@pts/14
root 30365 30344 0 Feb21 pts/14 00:00:02 -bash
root 30393 3382 0 Feb21 ? 00:00:10 sshd: root@pts/15

Where:

 -e to display all the processes.


 -f to display full format listing.

2. List the processes based on PIDs or PPIDs (ps -p, ps –


ppid)
The following method is used to get a list of processes with a particular PPID.

$ ps -f --ppid 9576
UID PID PPID C STIME TTY TIME CMD
root 9577 9576 0 Mar09 ? 00:00:00 /opt/tata/perl/bin/perl
/opt/tata/bin/tatad.pl
root 9579 9576 0 Mar09 ? 00:00:00 /opt/tata/perl/bin/perl
/opt/tata/bin/tatad.pl
root 9580 9576 0 Mar09 ? 00:00:00 /opt/tata/perl/bin/perl
/opt/tata/bin/tatad.pl
root 9581 9576 0 Mar09 ? 00:00:00 /opt/tata/perl/bin/perl
/opt/tata/bin/tatad.pl
root 9582 9576 0 Mar09 ? 00:00:00 /opt/tata/perl/bin/perl
/opt/tata/bin/tatad.pl
root 12133 9576 0 Mar09 ? 00:00:00 /opt/tata/perl/bin/perl
/opt/tata/bin/tatad.pl
The following example is to list the processes which has given PID.

$ ps -f -p 25009,7258,2426
UID PID PPID C STIME TTY TIME CMD
root 2426 4 0 Mar09 ? 00:00:00 [reiserfs/0]
root 7258 1 0 Mar09 ? 00:00:00 /usr/sbin/nscd
postfix 25009 7435 0 00:02 ? 00:00:00 pickup -l -t fifo -u

3. How to tell if my process is running in Unix?

You can list down all the running processes using [ps] command. Then you can “grep” your user name or

process name to see if the process is running. See below:

$>ps -e -o stime,user,pid,args,%mem,%cpu | grep "opera"


14:53 opera 29904 sleep 60 0.0 0.0
14:54 opera 31536 ps -e -o stime,user,pid,arg 0.0 0.0
14:54 opera 31538 grep opera 0.0 0.0

4. How to check the length of any line in a file?

We already know how to print one line from a file which is this:

$> sed –n '<n> p' file.txt

Where <n> is to be replaced by the actual line number that you want to print. Now once you know it, it is

easy to print out the length of this line by using [wc] command with '-c' switch.
$> sed –n '35 p' file.txt | wc –c

The above command will print the length of 35th line in the file.txt.

5. How to reverse a string in UNIX?

Pretty easy. Use the [rev] command.

$> echo "unix" | rev


xinu

PWD Command:
the pwd command (print working directory) is used to output the path of the current working directory.
How to copy files from one machine to another using ssh
[duplicate]

To copy a file from B to A while logged into B:

scp /path/to/file username@a:/path/to/destination

To copy a file from B to A while logged into A:


scp username@b:/path/to/file /path/to/destination

copy from one remote host to another

scp source_user@source_remote_host:/usr/bin/mysql_backup.sh
target_user@target_remote_host:/var/tmp/

LS commands
ls with no option list files and directories in bare format where we won’t be able to view details like file
types, size, modified date and time, permission and links etc.

# ls -l
total 176
-rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap
-rw-------. 1 root root 1586 Jul 31 02:17 anaconda-ks.cfg

List Files with Human Readable Format with


option –lh

# ls -lh
total 176K
-rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap
-rw-------. 1 root root 1.6K Jul 31 02:17 anaconda-ks.cfg

Sort Files by File Size


With combination of -lS displays file size in order, will display big in size first.

# ls -lS
total 176
-rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log
-rw-r--r--. 1 root root 46701 Jul 31 09:58 index.html
-rw-------. 1 root root 1586 Jul 31 02:17 anaconda-ks.cfg
-rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap

Вам также может понравиться