Friday 20 April 2012

UNIX Commands Part 1: File Maintenance tools


Log in to the UNIX-like operating system of your choice to get started. When you log in, you should automatically start in your user's home directory. The examples use the tuser (Test User) username. 

man: man stands for manual; 

$ man ls 

ls:

$ ls 
$ ls -l 
$ ls -a 
$ ls -R 

cd: 

$ cd Documents 
$ cd /tmp 
$ cd /home/tuser 

Special directory names 

$ cd .. 
$ cd ~ 

pwd:

$ pwd 

mkdir, rmdir:

$ mkdir TUTORIAL 
$ cd TUTORIAL 
$ pwd 
$ ls 

$ cd ~/TUTORIAL 
$ pwd 

Directory layout:

/home (or /users) 
/etc 
/bin 
/sbin 
/usr 
/car 
/tmp 

Files -

touch:

$ cd ~/TUTORIAL 
$ touch example.txt 

cp: 

$ cp example.txt /tmp/ 
$ ls /tmp/ 

$ cp /tmp/example.txt ./example2.txt 
$ ls 

mv:

$ mv example2.txt /tmp/. 

rm:

$ rm /tmp/example.txt 
$ rm /tmp/example2.txt 
$ ls /tmp/ 

Ownership and permissions 

chown, chgrp:

$ man chown 
$ man chgrp 

$ chown tuser example.txt 
$ chgrp admin example.txt 

chmod:

$ ls -l 
-rw-r--r--1 tuser admin 0 Aug 13 15:35 example.txt 
----------1 tuser admin 0 Aug 13 15:35 example.txt 

A file that has all permissions turned on reads this way: 

-rwxrwxrwx 1 tuser admin 0 Aug 13 15:35 example.txt 

$ man chmod 
$ chmod og-r example.txt 
$ ls -l 

You should see this result: 
-rw-------1 tuser admin 0 Aug 13 15:35 example.txt 

Dealing with multiple files  - 

$ cp example.txt example2.txt 
$ cp example.txt script.sh 
$ ls *.txt 
$ ls exa* 

Recursion:

$ cd ~ 
$ cp -R TUTORIAL /tmp/. 
$ ls /tmp/TUTORIAL/ 
$ rm -R /tmp/TUTORIAL/ 
$ ls /tmp/ 

Archives and compression - 

tar: 

$ cd ~ 
$ tar cvf /tmp/tutorial.tar TUTORIAL 
$ ls /tmp/ 

$ ls 
$ tar cvf tutorial 
$ ls 

$ rm -R /tmp/TUTORIAL 

gzip:

$ gzip tutorial.tar 
$ ls 

$ gzip -d tutorial.tar.gz 

The file system and file sizes:

df:

$ df -h 
$ ls -lh 

du:

$ cd ~ 
$ du -sk * 

$ du -sh * 

mount:

$ mount -t iso9660 /dev/cdrom /mnt/cdrom 
$ df 
$ ls /mnt/cdrom 

umount: 

$ umount /mnt/cdrom 
$ df 
$ ls /mnt/cdrom 

Redirection 
$ cd ~/TUTORIAL 
$ ls > listing.txt 
$ ls 

cat:

$ cat listing.txt 

more: 

$ ls /etc/ > listing2.txt 

$ cat listing2.txt 
$ more listing2.txt 

head and tail:

$ head listing2.txt 
$ tail listing2.txt 
$ head -n 2 listing2.txt 

grep:

$ grep host listing2.txt 

pipe:

$ ls /etc/ | grep host 
$ du -sh /etc/* | more 

No comments:

Post a Comment