Archive for December, 2008
How to resume broken download with wget
Dec 6th
Most of the times, due to network problems, the downloads will be broken if a big file is being downloaded. In such cases, the “wget” command can help in resuming the broken download using “-c” option. The following examples show how it can be done.
[neo@techpulp ~]# wget -c http://download.fedoraproject.org/pub/fedora/linux/releases/10/Live/i686/F10-i686-Live.iso
How to copy a directory recursively to a remote SSH host in a single command
Dec 6th
Here is the fancy command which creates backup of a local directory in a remote SSH host. The output of “tar” command at localhost is redirected to “tar” command at remote host for extraction. The best thing is it is all done with a single command. You can create an alias for such command if you think it is time consuming to type all that for easy usage.
With this command, I am storing backup of a directory named “myproject” in a remote host liz.techpulp.com in “/backup/” directory. After execution of this command, there will be a directory named “/backup/myproject” in More >
How to find a list of .c and .h files present in a directory recursively
Dec 6th
The “find” command can be used to get a list of .c and .h files recursively. The following example lists all .c and .h files present in the current directory and all sub-directories.
[neo@techpulp ~]# find . -type f -iname \*.[ch] ./myproject/test.c ./myproject/test.h ./src/examples/example1.c [neo@techpulp ~]#
How to uncompress .tar.gz2 archive in a single command
Dec 6th
The “tar” command supports a direct option to unompress .tar.gz files automatically. But it doesn’t support any such option for .tar.gz2 archives. So to avoid generating intermediate files, the output of “bzip2” command can be redirected to “tar” command as shown below.
[neo@techpulp ~]# bzip2 -dc myproject.tar.bz2 | tar -x
How to create .tar.bz2 file in a single command
Dec 6th
The tar command doesn’t support any option for running bzip2 automatically after creating taball. The following example shows how to create the archive with .tar.bz2 in a single command without creating intermediate archives. The output of “tar” is redirected to “bzip2” command to generate the target archive.
[neo@techpup ~]# tar -c myproject/ | bzip2 > myproject.tar.bz2
How execute a command in a separate directory without changing current working directory in Bash
Dec 6th
The following example shows how to move to a directory before executing a command and then automatically return back to the old diretory. This effectively runs the command with current directory as a different directory.
[neo@techpulp ~]# pwd /home/neo [neo@techpulp ~]# (cd /; ls) bin boot dev etc home lib lost+found media mnt opt proc root sbin selinux srv sys tmp usr var [neo@techpulp ~]# pwd /home/neo [neo@techpulp ~]#
As you can see, the commands are grouped in parenthesis so that the current directory remains same after running the commands.
How to list files by date in Linux
Dec 6th
The “ls -lt” command can be used to get the list of files sorted by their modification date.
[neo@techpulp ~]# ls -lt total 60 -rw-rw-r-- 1 neo neo 122 2008-12-06 01:39 monitor.sh -rw-rw-r-- 1 neo neo 44 2008-12-06 01:33 1.sh -rw-rw-r-- 1 neo neo 10381 2008-12-06 00:53 ifconfig.pdf drwxrwxr-x 3 neo neo 4096 2008-12-05 20:06 workspace drwxr-xr-x 3 neo neo 4096 2008-12-05 02:35 Desktop drwxrwxr-x 3 neo neo 4096 2008-12-05 01:46 Examples drwxr-xr-x 2 neo neo 4096 2008-12-05 00:59 Download drwxr-xr-x 2 neo neo 4096 2008-12-04 22:11 Documents drwxr-xr-x 2 neo neo 4096 2008-12-04 22:11 Music drwxr-xr-x 2 neo neo 4096 More >
How to get the PID of current bash shell script
Dec 6th
A special variable “$$” can be used with in a shell script to retrieve self process ID (PID). This is useful for monitoring scripts to store their PID in a file for other maintenance script to identify it. The following script shows how it can be done.
#!/bin/bash # store my PID echo $$ > /var/run/monitor.pid while [ 1 ]; do echo I am monitoring something sleep 5s done
Some scripts use their PID to create unique temporary files along with some fixed prefix. The following example script shows how it is done.
#!/bin/bash TMPFILE=myfile.$$ ls -l > $TMPFILE
However the above method serves More >
How to change priority of a process with renice
Dec 6th
Unlike “nice” command, the “renice” command expects PID of a process and can be used to alter priority of an already running process. It can take multiple PIDs at a time to alter scheduling priority. However to increase priority of a process, one should have root privileges while reducing the priority can be done by any user. This all applies for the processes owned by self. The following example reduces scheduling priority (sets 10 as priority) of a background process with PID 987.
[neo@techpulp ~]# renice +10 -p 987
To alter scheduling priority of all processes owned by a specific user, the More >
Run a command with low/high priority in Linux
Dec 6th
The “nice” command can be used to alter the priority of a command executed. This commands takes an optional argument with -n option that specific niceness of process going to be created. This option expects an integer ranging from -20 (highest priority) to 19 (lowest priority). This is an optional parameter and its default value is 10. The following example shows how a new software installation using “yum” is set to low priority.
[neo@techpulp ~]# nice yum install Basket
In general a process created in Linux has nice value of 0. So you can use “nice” command to specify lower value to specify More >


Recent Comments