Saturday, January 16, 2010

DAY 1:LINUX OVERVIEW








Your first Linux commands

1. ls - short for list
ls lists the files in the current working folder. This is probably the first command to try out.
Examples:
ls
ls -al --color=yes
2. pwd - print name of current/working directory
pwd prints the fully resolved name of the current (working) directory.
3. cd - Change directory
cd stands for change (working) directory and that's what it does. The folder below you (unless you are in /, where there is no lower directory) is called "..".
To go one folder down:
cd ..
Change into the folder Documents in your current working directory:
cd Documents
Change into a folder somewhere else:
cd /pub/video
The / in front of pub means that the folder pub is located in the / (lowest folder).
The basic commands
1. chmod - Make a file executable
To make a file executable and runnable by any user:
chmod a+x myfile
7.2. df - view filesystem disk space usage
df -h Filesystem Size Used Avail Use% Mounted on
/dev/hda3 73G 67G 2.2G 97% /
tmpfs 2.0M 24K 2.0M 2% /mnt/.init.d
tmpfs 252M 0 252M 0% /dev/shm
The flags: -h, --human-readable Appends a size letter such as M for megabytes to each size.
7.3. du - View the space used by files and folders
Use du (Disk Usage) to view how much space files and folders occupy.

Example du usage: du -sh Documents/
409M Documents

7.4. mkdir - makes folders
Folders are created with the command mkdir:
mkdir folder
To make a long path, use mkdir -p :
mkdir -p /use/one/command/to/make/a/long/path/
Like most programs mkdir supports -v (verbose). Practical when used in scripts.
You can make multiple folders in bash and other shells with {folder1,folder2} :
mkdir /usr/local/src/bash/{old,new,dist,bugs}

The command rmdir removes folders.
7.5. passwd - changes your login password
To change your password in Linux, type:
passwd
The root user can change the password of any user by running passwd with the user name as argument:
passwd jonny
will change jonnys password. Running passwd without arguments as root changes the root password.
If you need to add several new users and give them password you can use a handy program like Another Password Generator to generate a large set of "random" passwords.
7.5.1. KDE
From KDE you can change your password by going:
K -> Settings -> Change Password
K -> Settings -> Control Center -> System Administration -> User Account
7.6. rm - delete files and folders, short for remove
Files are deleted with the command rm: rm /home/you/youfile.txt
To delete folders, use rm together with -f (Do not prompt for confirmation) and -r (Recursively remove directory trees): rm -rf /home/you/foo/
Like most programs rm supports -v (verbose).

7.7. ln - make symbolic links
A symbolic link is a "file" pointing to another file.
To make a symbolic link : ln /original/file /new/link
This makes /original/file and /new/link the same file - edit one and the other will change. The file will not be gone until both /original/file and /new/link are deleted.
You can only do this with files. For folders, you must make a "soft" link.
To make a soft symbolic link : ln -s /original/file /new/link
Example: ln -s /usr/src/linux-2.4.20 /usr/src/linux
Note that -s makes an "empty" file pointing to the original file/folder. So if you delete the folder a symlink points to, you will be stuck with a dead symlink (just rm it).

7.8. tar archiving utility - tar.bz2 and tar.gz
tar (manual page is a very handle little program to store files and folders in archives, originally made for tapestreamer backups. Tar is usually used together with gzip (manual page) or bzip2 (manual page), comprepssion programs that make your .tar archive a much smaller .tar.gz or .tar.bz2 archive.
kde
You can use the program ark (K -> Utilities -> Ark) to handle archives in KDE. Konqueror treats file archives like normal folders, simply click on the archive to open it. The archive becomes a virtual folder that can be used to open, add or remove files just as if you were working with a normal folder.
7.8.1. tar files (.tar.gz)
To untar files: tar xvzf file.tar.gz
To tar files: tar cvzf file.tar.gz filedir1 filedir2 filedir2...
Note: A .tgz file is the same as a .tar.gz file. Both are also often refered to as tarballs.
The flags: z is for gzip, v is for verbose, c is for create, x is for extract, f is for file (default is to use a tape device).
7.8.2. bzip2 files (.tar.bz2)
To unpack files: tar xjvf file.tar.bz2
To pack files: tar cvjf file.tar.bz2 filedir1 filedir2 filedir2...
The flags: Same as above, but with j for for bzip2
You can also use bunzip2 file.tar.bz2 , will turn it into a tar.
For older versions of tar, try tar -xjvf or -xYvf or -xkvf to unpack.There's a few other options it could be, they couldn't decide which switch to use for bzip2 for a while.
How to untar an entire directory full or archives?
.tar:
for i in `ls *.tar`; do tar xvf $i; done
.tar.gz: for i in `ls *.tar.gz`; do tar xvfz $i; dONE