Monday, May 21, 2007

Basic Commands on Linux

ls (Listing)
This command will show you the contents of a directory.
ls --> will show you the contents of the current directory.
ls /dir/name --> will show you the contents of a specified directory.
ls -l --> will show you a long listing containing ownership, permissions, time last modified, and size.
ls -a --> will show you all of the files in the directory, including . (dot , the current directory)
ls -al --> What do you think?
Note that . stands for the current directory and .. refers to the parent directory

cd (Change Directory)
This command will change your current working directory.
cd --> If you just type in cd, then you will be sent to your home directory. For example, /home/mlevan/
cd /dir/name --> This command will send you directly into the desired directory.
cd /var/log/ --> This will send us to the /var/log directory.
What about these commands :
cd .
cd ..

cp (CoPy)
cp filename1 filename2 this command will copy the first file into the second file
cp Amy.txt Garret.txt
Note that if Garret.txt is already a file, then it will be overwritten !! Be careful with this command.
cp -i Amy.txt Garret.txt
If Garret.txt exists, then this command will inquire if you want to overwrite the file.
If Garret.txt does not exist, then you will not be asked.
Note that you can also add directory names to this:
cp /home/mlevan/Amy.txt /home/guest/Garret.txt
You can also copy files to a directory :
cp file1 file2 fileN directory_name
cp Amy.txt Garret.txt temp/
Note that ~ can also represent your home directory. For example, say I want to copy a file from /home/guest1/booty to the temp directory in my account:
cp /home/guest1/booty/blah.txt ~/temp/

rm (ReMove)
The rm command will remove a file.
rm filename
If you type in rm -i filename , then you will be asked if you really want to remove the file.
It is virtually impossible to regain a file after it has been removed in this fashion.

mv (MoVe)
This is the "rename" command used in DOS.
This command moves one filename into another filename.
mv filename1 filename2
The above command automatically writes over filename2 with whatever was in filename1
mv -i filename1 filename2
The above command will inquire if you really want to move the file.
You can also move directories with this command,
mv dir_name1 dir_name2

touch
This command will create a file.
touch filename
If the file already exists, then touch will update the timestamp of the file.

mkdir (MaKe DIRectory)
This command will create a directory in your current working directory:
mkdir dir_name
You can create a directory anywhere using the full pathname... if you have permission:
mkdir /var/log/class

rmdir (ReMove DIRectory)
This command will remove an empty directory.
rmdir temp
If the directory is not empty, then pass the parameters r (recursive) and f (force) to the rm command.
The f parameter will force the removal, never inquiring if you want to remove any subsequent files or directories.
The r parameter will remove travel down any directories within the directory and remove all the files.
rm -rf dir_name
cat
This command will print out a text file.
cat filename
What happens if we pass two files to this command?
cat filename1 filename2
What happens if we don't pass any files to this command?
cat

Control-D or Control-C ?
Control-C terminates a program.
Control-D stops the current input. (Admittedly, this can also end a program)

The end.
(source: bootThe Joy of Penguins - thanks a lots)