Wednesday, September 11, 2013

Few useful UNIX commands: Miscellaneous

1. Copying multiple files using SCP(Secure Copy)

a) if copying only files
scp user@host:"file1 file2" <destination_path>

For example:
scp suresh@suresh.pc:"/home/suresh/one.txt /home/suresh/two.txt" .

b) if copying directories also
We just need to include the option "-r" (recursive) in this case.

eg: scp -r suresh@suresh.pc:"/home/suresh/one/ /home/suresh/two.txt" .

2. Get the absolute path of a file

Lets say you are in some directory and you want the complete path of a file in that directory. Normally to get this, we need to copy the directory path first (by pwd) and then copy the file name, which is a two step process.
Instead we can use following command, which gives the absolute path:

readlink -f <filename>

3. Toggle between current directory and previous cd'd directory
cd -

4. Disk Usage(du): Occupied disk space by files/directories

a. Show the size of each mentioned directory or file

du <direcotory/fileName>
eg: du TestServlet.java LogoutServlet.java
4    TestServlet.java
4    LogoutServlet.java



b. Show the size in human readable format:

du -h
eg: du -h TestServlet.java LogoutServlet.java
4.0K    TestServlet.java
4.0K    LogoutServlet.java


c. Get the total of a directory or file

du -sh
eg: du -sh TestServlet.java LogoutServlet.java
4.0K    TestServlet.java
4.0K    LogoutServlet.java


d. Get the total of all directories mentioned

du -csh
eg: du -csh TestServlet.java LogoutServlet.java
4.0K    TestServlet.java
4.0K    LogoutServlet.java
8.0K    total