Here some of the basic bash-shell commands. This post is the continuation of the last post I’ve shared about Linux commands. The Linux commands shown here covers file and directory management matters.
1. Linux Commands for Listing Files and Directories
General Form: ls [parameter]
- Displaying a basic listing: The ls command is the most basic form of commands to display files and directories.
Example
[php]
root@so7am:/# ls
0 dev initrd.img lib64 mnt root srv usr vmlinuz.old
bin etc initrd.img.old lost+found opt run sys var
boot home lib media proc sbin tmp vmlinuz
[/php]
2. To display hidden files with normal files and directories, use the parameter -a
Example:
[php]
root@so7am:/# ls -a
. boot home lib64 opt sbin usr
.. .cache initrd.img lost+found proc srv var
0 dev initrd.img.old media root sys vmlinuz
bin etc lib mnt run tmp vmlinuz.old
[/php]
3. To display the files and directories in long listing format(with details) use the parameter -l
Example:
[php]
root@so7am:/home# ls -l
total 0
-rw-r–r– 1 root root 0 Mar 22 20:30 t1.txt
-rw-r–r– 1 root root 0 Mar 22 20:30 t2.tx
[/php]
4. To display specific file with a long listing format, use the general form
ls -l [filename]
Example:
[php]
root@so7am:/home# ls -l t1.txt
-rw-r–r– 1 root root 0 Mar 22 20:30 t1.txt
[/php]
5. The ls command also can take wildcard characters and use them to match patterns within the filter.
- A question mark? to represent one character
- An asterisk * to represent any number of characters.
Example:
[php]
root@so7am:/home# ls -l t*
-rw-r–r– 1 root root 0 Mar 22 20:30 t1.txt
-rw-r–r– 1 root root 0 Mar 22 20:30 t2.txt
[/php]
Note: Here the command results in the display of files with its name starting with t.
2. Linux Commands for Managing Files and Directories
- Creating Files:
- General Form: touch [filename(s)]
- Use touch command to create multiple files with a single command.
- Example: To create Files file1.txt file2.txt and file3.txt:
[php]
root@so7am:/home# touch file1.txt file2.txt and file3.txt
root@so7am:/home# ls
file1.txt file2.txt file3.txt
[/php]
2. Copying Files
- The cp command is used to copy files in Linux.
- General Form: cp source [destination]
- Example:
[php]root@so7am:/home# cp t1.txt t2.txt
[/php]
3. Linking Files
- Files are linked if one has to maintain two or more than two copies of the same file. It is achieved by using the command ln. In Linux, two types of links are available, namely symbolic link and hard link.
- General Form: ln [type] [original_file] [copy_file]
- To create symbolic links, use the type parameter -s. Presence of symbolic link is shown by the symbol – > as shown below:
Example:
[php]
root@so7am:/home# ln -s t2.txt t3.txt
root@so7am:/home# ls -l
total 0
-rw-r–r– 1 root root 0 Mar 22 20:45 and
-rw-r–r– 1 root root 0 Mar 22 20:45 file1.txt
-rw-r–r– 1 root root 0 Mar 22 20:45 file2.txt
-rw-r–r– 1 root root 0 Mar 22 20:45 file3.txt
-rw-r–r– 1 root root 0 Mar 22 20:30 t1.txt
-rw-r–r– 1 root root 0 Mar 22 20:30 t2.tx
-rw-r–r– 1 root root 0 Mar 22 20:50 t2.txt
lrwxrwxrwx 1 root root 6 Mar 22 21:00 t3.txt -> t2.txt
[/php]
4. Renaming Files
- Renaming files in Linux would mean moving files. By using the move command mv, we can rename the file.
- General Form: mv source [destination]
- Example: To rename file file1.txt to f1.txt
[php]root@so7am:/home# mv file1.txt f1.txt[/php]
5. Deleting Files
- In Linux deleting files is called removing files. To remove files and directories rm command can be used.
- General Form: rm [parameter] [file/directory]
- Use the parameter -i to prompt to make sure that the command is intended to perform seriously.
3. Linux Commands for Managing Directories
- Creating Directories
- Use the mkdir command to create directories.
- General form: mkdir [filename]
- Deleting directories
- Use the rmdir to remove directories
- General form: rmdir [parameter] [dir_name]
- Use the parameter -r to remove directory containing files/directories recursively. The rm command would work for directories with no files or directories inside it. In such cases use the -r parameter.
4. Viewing File Contents
There are many ways to view files. Some of them are:
1. Viewing the file type
- For viewing the file type, use file command.
- Example:
[php]
root@so7am:/home# file t1.txt
t1.txt: empty
[/php]
2. Viewing the whole filename
- Using the cat command
- Discussed in the previous lab.
- Using the more command
- It displays a text file, but stops after it displays each page of data
- General form: more filename
- Using less command
- Works like more command but provides features for scrolling forward and backward through the text. It can display file’s contents before it completes reading the whole file.
- General form: less filename
5. Sorting Data
- The sort command is used for sorting data in Linux. It take many parameters like -b, -i, -n, -o, -r etc.
- General Form: sort [parameter] filename
- Example, to sort data based on numerical values (treating numbers as numbers instead of characters) use -n parameter.
[php]root@so7am:/home# cat > t1.txt
1
5
6
5
root@so7am:/home# sort -n t1.txt
1
5
5
6[/php]
6. Viewing dates
-
- Using the date command
- The date command displays date details based on the parameter given.
- General Form: date [parameter]
- Basic date command displays time, time-zone date, month and year.
- Using the cal command
- The cal command displays calendar based on the parameter given.
- General form: cal [parameter]
- The parameter year (1-9999) displays the year’s calendar. Example: cal 2018 displays calendar of 2018.
- Using the date command