Linux Basic Commands

Linux Basic Commands

1. ls command

In Linux, the command "ls" is one of the most commonly used. It's used to display a list of files and sub-directories in the current directory.

A Linux command has the following basic syntax:

ls [ Options ] [File]
OptionsDescription
ls -alist all files including hidden file starting with '.'.
ls -dlist directories - with ' */'.
ls -llist with long format - show permissions.
ls -FAppend indicator (one of */=>@) to entries.
ls -lhThis command will show you the file sizes in human readable format.
ls -rlist in reverse order.
ls -ilist file's inode(index) number.
ls -ltrView Reverse Output Order by Date.
ls -tsort by time & date.
ls -nIt is used to print group ID and owner ID instead of their names.
ls -mA list of entries separated by commas should fill the width.
ls -gThis allows you to exclude the owner and group information columns.
ls -qForce printing of non-graphic characters in file names as the character `?';.
ls -QPlace double quotations around the entry names.

The 'ls' command is used to list files and directories

The contents of your current working directory, which is just a technical way of stating the directory that your terminal is presently in, will be listed if you run the "ls" command without any further options.

ls

Display the hidden files and directories

Use the -a option of the ls command to show hidden files and directories in the current directory.

ls -a

The files that start with the dot are hidden (.). The current directory (.) as well as the parent directory (..) are displayed by "ls -a".

Display complete information about the files

The "ls -l" option displays the contents of the current directory in a long listing format, one per line. The line begin with the file or directory permission, owner and group name, file size, created/modified date and time, file/folder name as some of the attributes.

ls -l

drwxrwxr–x 3 ubuntu ubuntu 4096 May 10 14:13 article_images

1st Character – File Type: The first character specifies the type of the file.
In the example above the hyphen (-) in the 1st character indicates that this is a normal file. Following are the possible file type options in the 1st character of the ls -l output.
Field Explanation

  • – normal file

  • d : directory

  • s : socket file

  • l : link file

  • Field 1 – File Permissions: Next 9 character specifies the permission of the file. Every 3 characters specify read, write, and execute permissions for the user(root), group and others respectively in order. Taking the above example, -rw-rw-r– indicates read-write permission for the user(root), read permission for the group, and no permission for others respectively. If all three permissions are given to the user(root), group and others, the format looks like -rwxrwxrwx

  • Field 2 – Number of links: The second field specifies the number of links for that file. In this example, 1 indicates only one link to this file.

  • Field 3 – Owner: The third field specifies the owner of the file. In this example, this file is owned by the username ‘maverick’.

  • Field 4 – Group: The fourth field specifies the group of the file. In this example, this file belongs to the ”maverick" group.

  • Field 5 – Size: The fifth field specifies the size of the file in bytes. In this example, ‘1176’ indicates the file size in bytes.

  • Field 6 – Last modified date and time: The sixth field specifies the date and time of the last modification of the file. In this example, ‘Feb 16 00:19’ specifies the last modification time of the file.

  • Field 7 – File name: The last field is the name of the file. In this example, the file name is 1.c.

2. pwd command

pwd stands for Print Working Directory. It prints the path of the working directory, starting from the root.
pwd is shell built-in command(pwd) or an actual binary(/bin/pwd).
$PWD is an environment variable which stores the path of the current directory.
This command has two flags.

pwd -L*: Prints the symbolic path.
pwd -P: Prints the actual path.*

The pwd command uses the following syntax:

pwd [option]

It has two acceptable options:

  • -L or –logical prints environment variable content, including symbolic links.

  • -P or –physical prints the actual path of the current directory.

Exit Status

The pwd command has two exit status:

  • 0. Success.

  • Non-zero. Failure.

$ pwd
/home/linuxconfig
$ pwd -P
/var/log
$ pwd -L
/home/linuxconfig/myfile

3. mkdir command

mkdir command in Linux allows the user to create directories (also referred to as folders in some operating systems ). This command can create multiple directories at once as well as set the permissions for the directories. It is important to note that the user executing this command must have enough permissions to create a directory in the parent directory, or he/she may receive a ‘permission denied’ error.

Syntax:

mkdir [options...] [directories ...]
  • –version: It displays the version number, some information regarding the license and exits.
    Syntax:
mkdir --version
  • Output:

mkdir --version screenshot

  • –help: It displays the help related information and exits.
    Syntax:
mkdir --help
  • Output:

mkdir --help OUTPUT

  • -v or –verbose: It displays a message for every directory created.
    Syntax:
mkdir -v [directories]
  • Output:

mkdir -v

  • -p: A flag which enables the command to create parent directories as necessary. If the directories exist, no error is specified.

    Syntax:

mkdir -p [directories]
  • Suppose you execute the following command –
mkdir -p first/second/third
  • If the first and second directories do not exist, due to the -p option, mkdir will create these directories for us. If we do not specify the -p option, and request the creation of directories, where parent directory doesn’t exist, we will get the following output –

  • Error when -p option is not specified

  • Thank you all for giving your valuable time for reading. Any feedback or remarks will be highly appreciated.

  • See you in the next blog.😁✌️