Basic Unix / Linux command for newbies
By Jayita, Gaea News NetworkThursday, February 4, 2010
To teach a newbie UNIX command is not an easy task, because it’s a vast thing to learn. But, what I am trying is to show you the first step to UNIX.
UNIX is an operating system, first developed in 1960s. It has been under constant development, ever after. It is a stable, multi-user, multi-tasking system for servers, desktops and laptops.
UNIX operating system consists of three parts — Kernel, Shell and the programs.
Kernel is the hub of the operating system: it allocates time and memory to programs and handles the filestore and communications in response to system calls.
The shell acts as an interface between the user and the kernel. When a user logs in, the login program checks the username and password, and then starts another program called the shell.
Basic Commands
When you first login, your current working directory is your home directory. Your home directory has the same name as your user-name, for example, jayita@servername. Your personal files and subdirectories are saved here.
To find out what is in your home directory, type
list directory contents
ls
To list all files and directories in your home directory
ls -a
It also includes those whose names begin with a dot.
Make a directory
To make a subdirectory in your home directory, you have to type mkdir followed by subdirectory name. For eg
mkdir unixabc
See the directory you have created, type
ls
Change directory
To change the directory you have created, type cd followed by directory name. eg,
cd unixabc
The current directory
To see the current directory, type cd, then a space, then the dot.
cd .
The parent directory
If you want to go parent to the current directory, type cd, space, double dots.
cd ..
Note: typing cd without argument will return you to home directory.
To know full pathname, type
pwd
It will show you the absolute pathname of your home directory.
eg, /home/tmp/usr/ur1. It means ur1(home directory)is in the sub-directory urs, which in turn is locate in tmp sub-directory, which is in the home sub-directory, which is in the top-level root directory called ”/”.
Copy file
To copy a file named f1 into another file f2.
cp f1 f2
Move or rename file
To move or rename a file f1 to another file f2.
mv file1 file2
Remove a file
rm f2
Concatenate
The command cat can be used to display the contents of a file on the screen. Type:
cat f3
The Wildcard Characters
Use of (*)
The special characters * and ? are called wildcard character in Unix.
The * is used to match none or more character(s) in a file (or directory)
ls inst*
This command will list all files in the current directory starting with inst….
ls *at
This command will list all files in the current directory ending with …at
Use of (?)
The character ? match exactly one character, i.e ?at will match files like cat, hat, fat, sat, bat etc.
ls ?at
Manuals
man command is used to read the manual page for a particular command. For eg, to find about wc (word count) command, type
man wc
similarly you can use
what is wc
Note: what is command gives one-line description, but omits details information.
- Pipe Character
This character is used in linux command line between two command to take output from the first command and use it as input of the second command.
* cat log.txt | grep FAILURE
In the above command, 1st it will show you the full content of log.txt. It is the output of the 1st command. Then it will take the content of log.txt and grep you the failures from the above command i.e to say it will use the output of the 1st command as input of the 2nd command.
- && and Command
If there is && character between two cammand, then it means if the 1st command run successfully then run the second command.
* cat log.txt && grep FAILURE
Here it means that if command line can show you the full content of log.txt file then, grep the failures from the file.
- || or Command
It means or. If there is || character between two cammand, then it means if the 1st command failed to run successfully then run the second command.
- ; Command separater
It is command separater. If there is || character between two cammand, then it means that two commands are not dependent to each other, they will run separately.
- > Overwrite
This character is used to overwrite the existing file by the specified one.
* cat log.txt | grep FAILURE > failure.txt
In the above command, 1st it will show you the full content of log.txt. Then it will take the failures from the above command and overwrite in failure.txt file.
- >> Append
It functions same as ‘ > ‘ but it save the content at the end of the file instead of overwriting it.
- < Read
If you give this charecter before a file name, it will read the whole content of the file.
- * echo < file.txt
This command will print the whole content of the file.txt.
VI editor
The VI editor is a screen-based editor used in Unix. The VI editor has powerful features to aid programmers, but many beginners avoid using VI because the different features overwhelm them. I am mentioning only some basic features of vi editor.
1. How to search in vi editor??
You can search by typing front slash ‘/ ‘ followed by your search keyword and press Enter. For search next press n. To exit from search, press escape Esc.
2. How to quit from vi editor??
There are three type of quits =>
* Suppose you don’t make any change in the file. In this case you can quit from vi editor by typing
:q and press Enter
* Suppose you make some changes in the file and you don’t want to save the changes. In this case you can quit from vi editor by
press escape Esc, then type :q! and press Enter
This is called force quit.
* If you want to save the changes and then quit do the following
step1: press escape Esc, then type :w and press Enter
step2: press escape Esc, then type :q and press Enter
3. How to save file written in vi editor??
press escape Esc, then type :w and press Enter
4. How to insert or delete data in file open in vi editor??
step1: press I(prefered) or A and move the mouse cursor at the position where you want to insert or delete and then insert data or delete data
step2: press escape Esc, then type :w and press Enter step3: press escape Esc, then type :q and press Enter
5. How to delete a line without insert mode in file open in vi editor??
Move the mouse cursor on the line to be deleted and press DD
6. How to delete a character without insert mode in file open in vi editor??
Move the mouse cursor on the character to be deleted and press X
7. How to go on a particular line number in file open in vi editor??
Press : followed by line number
8. How to go first line of the file open in vi editor??
Press :1
9. How to go last line of the file open in vi editor??
Press :$
Conclusion
I have tried to gather some preliminary commands of Unix/Linux. These are only for the newbies, to give them a feel of what’s actually the UNIX commands are. The more you need, the more you learn, there is no other way. You can also go for online tutorials. Plenty of good books are there to help you mastering UNIX command.
February 6, 2010: 11:30 pm
This is a great introduction… I even learned some new stuff I never knew before and some stuff I did know… but always forget to use. Also… There were a couple of minor errors: Thanks again! Now I can link people to this instead of writing one on my own. |
netjunki