Академический Документы
Профессиональный Документы
Культура Документы
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
$>sed -i'' '10 i This is the new line' file.txt # i stands for insert
Using command, we can do it in many ways. Based on what we have learnt so far, we can make use of [ls]
To check the status of last executed command in UNIX, you can check the value of an inbuilt bash variable
$> echo $?
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:
$ 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
You can list down all the running processes using [ps] command. Then you can “grep” your user name or
We already know how to print one line from a file which is this:
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.
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]
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
# 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
# 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