Friday, 14 June 2013

Unix Commands And Info

Switch to 'root' User in SUDO

Type:
sudo su -

Prompt should change to reflect the fact you're now logged in as root.

If the following text appears, you don't have root access:



Email a File from Unix

To email a binary/data file from Unix use:
uuencode <filename> <name-to-call-attachment> | mail -s "Subject" <email address>

If uuencode isn't available the contents of a text file can be emailed from Unix:
mail -s "Subject" user@example.com(Note: The contents of the file appears in the body of the email message, not as an attachment)


Search for Files by Contents

To find a file based on its contents in Unix:
find . -name "*.txt" -exec grep -il "hello" {} \;

To scan all files in the current folder for particular text and return the filename:
grep -il "hello"
('i' = ignore case, 'l' = return file names of matches)



Find files and Delete Them

The following command finds files named 'trash' and then deletes them:
find . -name "trash" -exec rm {}\;



Find a file By Date

To find a file based on its date (in this case more than 365 days) and move it:
find . -mtime +365d -exec mv {} /new/destination/folder \;



Copy Hidden Files in Unix


The following command copies any hidden files (files starting with full stop, dot, '.') followed by any letter or number:

cp /path/to/source/.[a-zA-Z0-9]* /path/to/target/




Sort Directory Listing

(Tested on AIX)

To sort a directory listing in Unix:
ls -l | sort +4 -rn

Sorts the list by column 4 (file size) in reverse order (smallest files listed last).


See Who Is or Was on a Server

Type: last | more


Create Softlink (Symbolic link)

ln -s <destination folder> <alias>

The '-s' switch is required to link across filesystems.
More info here:
http://pic.dhe.ibm.com/infocenter/aix/v7r1/index.jsp?topic=%2Fcom.ibm.aix.cmds%2Fdoc%2Faixcmds3%2Fln.htm