If you know the proper commands, File Management becomes easier.
Following are the basic Linux commands with examples.
1) How to list files?
Run 'ls' command to get the list of files in your UNIX or Linux system.
It gives the list of files /directories in your current directory.
Run the command 'ls -R' to get all the files in directories and subdirectories.
Note: If you enter, "ls - r" you will get an error.
You can use 'ls -al' command for getting complete information on the files.
The command provides information in a columnar format. The columns provide the following information:
1st Column File type and access permissions
2nd Column # of HardLinks to the File
3rd Column Owner and the creator of the file
4th Column Group of the owner
5th Column File size in Bytes
6th Column Date and Time
7th Column Directory or Filename
2) How to List the Hidden Files?
To view the hidden files, use the following command.
ls -a
In UNIX/Linux, hidden items will begin with a "." of the file or directory.
3) How to create and view those files?
a) To create a new file, you can use the command.
cat
1) cat > filename
2) Add content
3) Press 'Ctrl + d' to return to the command prompt.
The "cat" command is also be used for copying, combining and creating new text files.
b) To view a file, use the command -
cat filename
c) The syntax to combine two files is -
cat file1 file2 > newfilename
As soon as you entered this command and ran, the files concatenated, but you can't see a result because Bash Shell is the silent type. Shell Commands will never give you a confirmation message like "OK" or "Command Successfully Executed". It will only show a notification when something goes wrong or when an error has occurred.
To get the new combo file, use the command:
cat sample
Note: By using the above command, only text files can be displayed and combined.
4) How to delete a file?
Use 'rm' command to delete the files from the system without confirmation.
To remove a file, use the below syntax -
rm filename
5) How to move and rename the file?
Use the following command to move a file.
mv filename new_file_location
Please note that: "mv" command requires superuser permission. A standard user can't remove the file by running "mv" command, and you will get an error. To overcome the error, run the below command:
sudo command_you_want_to_execute
Sudo program allows regular users to run programs with the security privileges of the superuser or root. Sudo command will ask for password authentication. Though, you do not need to know the root password. You can supply your password. After authentication, the system will invoke the requested command.
For renaming file use the following command:
mv filename newfilename
6) Directory Manipulations
Let's learn some directory manipulation Linux basic commands.
Directories can be created on a Linux operating system using the following command:
mkdir directoryname
It will form a subdirectory in your current working directory, which is usually your "Home Directory".
Use the below command, if you want to create a directory in a different location other than 'Home directory':
mkdir
You can also create more than one directory at a time.
7) How to remove directories?
You can remove a directory by running the below command:
rmdir directoryname
Note: Make sure that there is no file/subdirectory under the directory you want to delete. Delete the files/sub-directory first before deleting the parent directory.
8) How to rename a directory?
The 'mv' (move) command can be used to rename the directories. The format shows below.
mv directoryname newdirectoryname
Other Important Commands
a) The 'Man' command
To receive guidance on any command that you do not know, you can type
man
It is similar to 'HELP' file found in popular software.
b) The History Command
History command displays all the basic commands in Linux that you have practised in the past for the current terminal session.
c) The clear command
This command will remove all the clutter on the Terminal and give you a clean window to work on, just like when you start the Terminal.
d) Pasting commands into the Terminal
Many times you would have to enter in large commands on the Terminal. It can be not very pleasant at times, and if you require to ignore such a situation, copy, paste the command can come to save.
For copying, you would use "Ctrl + c", but for pasting it on the Terminal, you have to use "Ctrl + Shift + p". You can also use "Shift + Insert or select Edit>Paste" on the menu.
'pr' command
There are many options possible with this command which help in getting required format settings on File. The most accepted 'pr' options are listed below.
Option Function
-x Divides the data into 'x' columns
-h "header" Assigns "header" value as the report header
-t Does not print the header and top/bottom margins
-d Double spaces the output file
-n Denotes all line with numbers
-l page length Defines the lines (page length) in a page. Default is 56
-o margin Formats the page by the margin number
1) Arranging data into columns
For dividing the File, the syntax is
pr -x Filename
The '-x' option with the 'pr' command divides the data into x columns.
2) Assigning a header
The syntax is:
pr -h "Header" Filename
The '-h' options assign "header" value as the report header.
3) Indicating all lines with numbers
The syntax is:
pr -n Filename
Above are some of the 'pr' command choices you can use to adjust the file format.
Printing a file
If you want to get a hard copy of the File once you have done the formatting, run the following command:
lp Filename
orlpr Filename
Installing Software
Using Linux/Unix basic commands, installation files in Linux are distributing as packages. But the package contains only the program itself. Any dependent segments will have to be installed separately, which are usually available as packages themselves.
Run the appropriate commands to install or delete a package. You can update all the installed packages in our system applying following command -
sudo apt-get update
Linux Mail Command
To send mails via a terminal, you need to install packages 'mailutils'.
The command syntax is -
sudo apt-get install packagename
Once done, you can then use the following syntax for giving an email.
mail -s 'subject' -c 'cc-address' -b 'bcc-address' 'to-address'
Press Ctrl+D you are completed composing the mail. The mail will send to the specified address.