Basic Linux commands

Basic Linux commands

Day 2/90

Listing commands

ls: This command lists all files and directories in the current working directory

$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

ls -l: This command lists files and directories in a long format, including file permissions, ownership, size, and modification date.

$ ls -l
total 40
drwxr-xr-x  2 user user 4096 May  8 11:30 Desktop
drwxr-xr-x  2 user user 4096 May  8 11:30 Documents
drwxr-xr-x  2 user user 4096 May  8 11:30 Downloads
drwxr-xr-x  2 user user 4096 May  8 11:30 Music
drwxr-xr-x  2 user user 4096 May  8 11:30 Pictures
drwxr-xr-x  2 user user 4096 May  8 11:30 Public
drwxr-xr-x  2 user user 4096 May  8 11:30 Templates
drwxr-xr-x  2 user user 4096 May  8 11:30 Videos

ls -a: This command lists all files and directories, including hidden files and directories starting with a dot.

$ ls -a
.   .bash_history  .bashrc  Desktop   Documents  Downloads
..  .bash_logout   .cache   Music     Pictures  Public
    .bash_profile  .config  Templates Videos

ls -lh: This command lists files and directories in a long format with human-readable file sizes.

$ ls -lh
total 40K
drwxr-xr-x  2 user user 4.0K May  8 11:30 Desktop
drwxr-xr-x  2 user user 4.0K May  8 11:30 Documents
drwxr-xr-x  2 user user 4.0K May  8 11:30 Downloads
drwxr-xr-x  2 user user 4.0K May  8 11:30 Music
drwxr-xr-x  2 user user 4.0K May  8 11:30 Pictures
drwxr-xr-x  2 user user 4.0K May  8 11:30 Public
drwxr-xr-x  2 user user 4.0K May  8 11:30 Templates
drwxr-xr-x  2 user user 4.0K May  8 11:30 Videos

ls -R: This command lists files and directories recursively, including all subdirectories.

$ ls -R
.:
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

./Desktop:

./Documents:

./Downloads:

./Music:

./Pictures:

./Public:

./Templates:

./Videos:

ls -t: This command lists files and directories sorted by modification time, with the most recently modified files or directories listed first.

$ ls -t
Downloads  Pictures  Documents  Videos  Desktop  Templates  Music  Public

ls *.sh: the command ls *.sh to list all files with a ".sh" extension:

$ ls *.sh
file2.sh file3.sh

ls -i to list files and directories with their corresponding inode numbers:

$ ls -i
166501 Desktop  166553 Downloads  166500 Documents  166499 Music
166502 Pictures  166498 Public  166554 Templates  166497 Videos

Directory commands:

pwd: Print the present working directory.

$ pwd
/home/user/Documents

cd path_to_directory: Change the directory to the provided path.

$ cd /home/user/Documents

cd ~ or just cd: Change the directory to the home directory.

$ cd ~

cd -: Go to the last working directory.

$ cd -

cd ..: Change directory to one step back.

$ cd ..

cd ../..: Change directory to 2 levels back.

$ cd ../..

To create a new directory called "newFolder" in the current working directory, you can use the following command:

$ mkdir newFolder

To create a hidden directory called ".NewFolder" in the current working directory, use the following command:

$ mkdir .NewFolder

To create multiple directories called "A", "B", "C", and "D" in the current working directory, you can use the following command:

$ mkdir A B C D

To create a new directory called "Mydirectory" in the specific location /home/user/, use the following command:

$ mkdir /home/user/Mydirectory

To create a nested directory structure with directories called "A", "B", "C", and "D" inside each other, use the following command:

$ mkdir -p A/B/C/D
# The -p option tells mkdir to create any necessary parent directories that don't already exist. This way, you can create a whole nested structure with one command.