LINUX COMMANDS AND FILTERS
Some Very Useful, Basic Commands
The first command you learn in any OS – is display files in a directory: in DOS. You would type “dir” to display the contents of a folder. In Linux, the equivalent is the “Ls” command. This command displays the files and folders in the current directory. The “Ls” command comes with some extra options; “Ls – l” produces a “long list” that shows the owner, group size. Date modified and permission. “LS – a” will list hidden files as well. In DOS. This is done by “dir fa”. There are other options- which we will leave to you to explore.
Listing Files and Directories
Ls ( list )
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, mystyleminx and it is where your personal files and sub directories are saved.
To Find out what is in your home directory, type
% ls ( short for list )
The Ls command lists the contents of your current working directory.
There may be no files visible in your home directory in which case, the UNIX prompt will be returned. Alternatively, there may already be some files inserted by the System Administrator when your account was created.
Ls does not, in fact, cause all the files in your home directory to be listed, but only those one whose name does not begin with a dot ( . ) Files beginning with a dot ( . ) are known as hidden files and usually contain important program configuration information. They are hidden because you should not change them unless you are very familiar with UNIX!!!
To list all files in your home directory including those whose names begin with a dot, type
% ls –a
Ls is an example of a command which can take options: -a is an example of an option. The options change the behavior of the command. There are online manual pages that tell you which options a particular command can take, and how each option modifies the behavior of the command.
Wildcards
The characters * and ? : The character * is called a wildcard, and will match against none or more character(s) in a file (or directory ) name. For example, in your mystyleminx directory, type
% ls list*
This will list all files in the current directory starting with list…..
Try Typing
% ls list*
This will list all files in the current directory ending with list…..
The character ? will match exactly one character. So ls ?ouse will match files like house and mouse, but not grouse.
Chmod ( changing a file mode )
Only the owner of a file can use chmod to change the permissions of a file. The options of chmod are as follows:
Symbol Meaning
U user
G group
O other
A all
R read
W write (and delete)
X execute (and access directory)
+ add permission
- Take away permission
For example, to remove read write and execute permissions on the file mystylepro for the group and other, type
% chmod go-rwx mystylepro
This will leave the other permission unaffected.
To give read and write permissions on the file mystylepro to all.
% chmod a+rw mystylepro
Making Directories
mkdir ( make directory )
We will now make a subdirectory in your home directory to hold the files you will be creating and using. To make a subdirectory called mystyleminx in your current working directory type
% mkdir mystyleminx
To see the directory you have just created, type
% ls
Changing to a different directory
cd ( change directory )
The command cd directory means change the current working directory to ‘directory’. The current working directory may be thought of as the directory you are in, i.e. your current position in the
file - system tree.
To change to the directory you have just made, type
% cd mystyleminx
Type ls to see the contents (which should be empty)
The Directories . and ..
In UNIX, ( . ) means the current directory, so typing
% cd .
( ..) meant the parent of the current directory, so typing
% cd ..
will take you one directory up the hierarchy (back to your home directory).
Pathnames
pwd (print working directory)
Pathnames enable you to work out where you are in relation to the whole file – system.
For example, to find out the absolute pathname of your home – directory, type cd to get back to your home – directory and then type
% pwd
The full pathname will look something like this:
/home/student/poly/mystyleminx
Which means that mystyleminx (your home directory) is in the directory poly, which is located on the student under home directory.
Note:
Ls List files and directories
Ls – a List all files and directories
Mkdir make a directory
cd directory change to named directory
cd change to home – directory
cd ~ change to home – directory
cd.. change to parent directory
pwd display the path of the current directory
Copying Files
cp (copy)
cp file1 file 2 is the command which makes a copy of file1 in the current working directory and calls it file2
What we are going to do now, is to take a file stored in an open access area of the file system, and use the cp command to copy its to your mystyleminx directory.
% cp/home/student/file.txt /home
The above command means copy file1.txt from /home/student to the /home directory, keeping the same name.
Moving files
mv (move)
mv file1 file2 moves ( or renames ) file1 to file2
To move a file from one place to another, use the mv command. This has the effect of moving rather than copying the file, so you end up with only one file rather that two.
It can also be used to rename a file, by moving the file to the same directory, but giving it a different name.
We now move the file mystylepro.bak to backup directory.
% mc mystylepro.bak backups/.
Removing Files and Directories
rm (remove), rmdir (remove directory)
To delete (remove) a file, use the rm command. As an example, we are going to create a copy of the mystylepro.txt file then delete it.
% cp mystylepro.txt tempfile.txt
% ls (to check if it has created the file )
% rm tempfile.txt
% ls (to check if it has deleted the file)
Displaying the contents of a file on the screen.
Clear ( clear screen )
Before you start the next section, you may like to clear the terminal windows of the previous commands so the output of the following commands can be clearly understood.
At the prompt, type
% clear
This will clear all text and leave you with the % prompt at the top of the window.
Cat ( Concatenate )
The command cat can be used to display the contents of a file on the screen. Type:
% cat mystylepro.txt
As you can see, the file is longer than the size of the window, so it scrolls past making it unreadable.
wc ( Word Count )
A handy little utility is the wc command, short for word count. To do a word count on mystylepro.txt, type
% wc –w mystylepro.txt
To find out how many line the file has, type
% wc –l mystylepro.txt
Redirection
Redirecting the Output (Outward Redirection): We use the > symbol to redirect the output of a command. For example, to create a file called list1 containing a list of students, type:
% cat > list1
Then type in the names of some students. Press [Return] after each one.
Anmol
Deependra
Aditya
^D (control D to stop and Save )
What happens is the cat command reads the standard input ( the keyboard ) and the > redirects the output, which normally goes to the screen, into a file called list1
To read the contents of the file, type
% cat list1
Append To File
The form >> appends standard output to a file. So to add more items to the file list1, type
% cat >> list1
Then type in the names of more students
Aslam
Anil
Ankita
Amit
Anjali
^D (control to stop and save)
To read the contents of the file, type
% cat list1
You should now have two files. One contain Three names of students, the other contains five students. We will now use the cat command to join (concatenate ) list1 and list2 into a new file called mystyleinside. Type
% cat list1 list2 > mystyleinside
What this is doing is reading the contents of list1 and list2 in turn, then out putting the text to the file mystyleinside
To read the contents of the new file, type
% cat mystyleinside
Redirecting the Input ( Inward Redirection )
We use the < symbol to redirect the input of a command.
The command sort alphabetically or numerically sorts a list. Type
% sort
Then type in the names of some students. Press [Return] after each one.
Zoro
Love
Mintu
^D (control to stop and save)
The Output will be
Love
Mintu
Zoro
Using < you can redirect the input to come from a file rather than the keyboard. For example, to sort the list of students, type
% sort < mystyleinside
And the sorted list will be output to the screen.
To output the sorted list to a file, type,
% sort < mystyleinside > list
Use cat to read the contents of the files list
No comments:
Post a Comment