Basic Bash Shell Commands

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]

  1. 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.

  1. A question mark? to represent one character
  2. 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

  1. Creating Files:
    1. General Form: touch [filename(s)]
    2. Use touch command to create multiple files with a single command.
    3. 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

  1. The cp command is used to copy files in Linux.
  2. General Form: cp source [destination]
  3. Example:

    [php]root@so7am:/home# cp t1.txt t2.txt

    [/php]

 

3. Linking Files

  1. 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.
  2. General Form: ln [type] [original_file] [copy_file]
  3. 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

  1. Renaming files in Linux would mean moving files. By using the move command mv, we can rename the file.
  2. General Form: mv source [destination]
  3. Example: To rename file file1.txt to f1.txt

[php]root@so7am:/home# mv file1.txt f1.txt[/php]

 

5. Deleting Files

  1. In Linux deleting files is called removing files. To remove files and directories rm command can be used.
  2. General Form: rm [parameter] [file/directory]
  3. Use the parameter -i to prompt to make sure that the command is intended to perform seriously.

3. Linux Commands for Managing Directories

  1. Creating Directories
    1. Use the mkdir command to create directories.
    2. General form: mkdir [filename]
  2. Deleting directories
    1. Use the rmdir to remove directories
    2. General form: rmdir [parameter] [dir_name]
    3. 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

  1. For viewing the file type, use file command.
  2. Example:

[php]

root@so7am:/home# file t1.txt

t1.txt: empty

[/php]

2. Viewing the whole filename

  1. Using the cat command
    1. Discussed in the previous lab.
  2. Using the more command
    1. It displays a text file, but stops after it displays each page of data
    2. General form: more filename
  3. Using less command
    1. 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.
    2. General form: less filename

5. Sorting Data

  1. The sort command is used for sorting data in Linux. It take many parameters like -b, -i, -n, -o, -r etc.
  2. General Form: sort [parameter] filename
  3. 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

    1. Using the date command
      1. The date command displays date details based on the parameter given.
      2. General Form: date [parameter]
      3. Basic date command displays time, time-zone date, month and year.
    2. Using the cal command
      1. The cal command displays calendar based on the parameter given.
      2. General form: cal [parameter]
      3. The parameter year (1-9999) displays the year’s calendar. Example: cal 2018 displays calendar of 2018.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *