Which command is used to list all the files in your current directory including hidden Mcq?

Last update on August 19 2022 21:50:34 (UTC/GMT +8 hours)

Introduction

In this session, we have covered an overview of the most common commands to work with directories : pwd, cd, ls, mkdir, rmdir. These commands are available on any Linux (or Unix) system. We have also discussed the absolute and relative paths and path completion in the bash shell.

pwd

Displays the full path-name for the current directory. Open a command line interface (like gnome-terminal, konsole, xterm, or a tty) and type pwd.

[email protected]:~$ pwd /home/datasoft

cd

You can change your current directory (working directory) with the cd command.

[email protected]:~$ cd /etc [email protected]:/etc$ pwd /etc [email protected]:/etc$ cd /bin [email protected]:/bin$ pwd /bin [email protected]:/bin$ cd /home/datasoft/ [email protected]:~$ pwd /home/datasoft

cd ~<

Typing cd without a target directory will return you to your home directory. Typing cd ~ has the same effect.

[email protected]:~$ cd /etc [email protected]:/etc$ pwd /etc [email protected]:/etc$ cd [email protected]:~$ pwd /home/datasoft [email protected]:~$ cd ~ [email protected]:~$ pwd /home/datasoft

To go to the parent directory (moves you up one directory), type cd ..

[email protected]:~$ pwd /home/datasoft [email protected]:~$ cd .. [email protected]:/home$ pwd /home

To stay in the current directory, type cd . ;-) We will see the useful use of the . character representing the current directory later.

cd -

Another useful shortcut with cd is to just type cd - to go to the previous directory.

[email protected]:~$ pwd /home/datasoft [email protected]:~$ cd /etc [email protected]:/etc$ pwd /etc [email protected]:/etc$ cd - /home/datasoft [email protected]:~$ cd - /etc

Absolute paths

Absolute paths begin at the top / (referred to as root) and then look down for the requested directory. The tree command below is used as an example for the purpose of explaining cd.

Which command is used to list all the files in your current directory including hidden Mcq?

Suppose you are currently in the user directory and you want to switch to the user1 directory, you need to move up in the directory tree.
If you type :

Which command is used to list all the files in your current directory including hidden Mcq?

While you are in the user directory, you will get an error message "No such file or directory". This is because there is no user1 directory below user directory.

To go to user1 directory, you would type :

Which command is used to list all the files in your current directory including hidden Mcq?

This is an absolute path. It tells linux to start at the top (here it is a home directory) and look down until it finds the user1 directory.

Therefore absolute paths will take you TO any directory, FROM any directory.

Relative paths

Any path that does not begin with a / or a ~ is a relative path. You can use an absolute path anywhere, a relative path can be used as well. Unlike an absolute path where your current location doesn't matter, it does matter where your current directory is, when using a relative path. Two special symbols "." (dot) and ".." (dot dot) are used to represent relative positions in the file system tree. The tree command below is used as an example for the purpose of explaining relative path.

Which command is used to list all the files in your current directory including hidden Mcq?

Let's change the working directory to /home/user

Which command is used to list all the files in your current directory including hidden Mcq?

Let's say that we want to change the working directory to /home/usre1 (see the above tree structure). We can do that two different ways. Either with an absolute pathname :

Which command is used to list all the files in your current directory including hidden Mcq?

Or, with a relative pathname :

Which command is used to list all the files in your current directory including hidden Mcq?

Therefore we get an identical result from two different methods.

Here is another example :

Which command is used to list all the files in your current directory including hidden Mcq?

We can change the working directory from /home to /home/user1 in two different ways. Either using an absolute pathname:

Which command is used to list all the files in your current directory including hidden Mcq?

Or, with a relative pathname:

Which command is used to list all the files in your current directory including hidden Mcq?

It is suggested that you can omit the "./". See the following command which will give you the identical result.

Which command is used to list all the files in your current directory including hidden Mcq?

Be aware of where you are

Always you should know which working directory you are in before you state the relative path to the directory or file you want to get to. You do not have to worry about your position in the file system, though, when you state the absolute path to another directory or file. If you are not sure, type pwd (print the name of the current directory).

path completion

The tab key can help you in typing a path without errors. Typing cd /et followed by the tab key will expand the command line to cd /etc/. When typing cd /Et followed by the tab key, nothing will happen because you typed the wrong path (upper case E).

You will need fewer key strokes when using the tab key, and you will be sure your typed path is correct!

ls

ls command is used to list names of files and sub-directories of a directory.

[email protected]:~$ ls abc.txt Documents examples.desktop Pictures Templates Videos Desktop Downloads Music Public typescript [email protected]:~$

ls -a

Lists all the files in the directory, including the hidden files (.filename).

[email protected]:~$ ls abc.txt Documents examples.desktop Pictures Templates Videos Desktop Downloads Music Public typescript [email protected]:~$ ls -a . .cache Downloads Pictures Videos .. .compiz examples.desktop .pki .Xauthority abc.txt .config .gconf .profile .xsession-errors .bash_history Desktop .ICEauthority Public .xsession-errors.old .bash_logout .dmrc .local Templates .bashrc Documents Music typescript [email protected]:~$ The .. and . at the top of your list refer to the parent directory and the current directory, respectively.

Each directory file has an entry for itself (the dot file (.)) and for its parent directory (the dot-dot file (.. ). The -a option can be combined with -l option.

ls -l

Lists details about contents, including permissions (modes), owner, group, size, creation date, whether the file is a link to somewhere else on the system and where its link points.

[email protected]:~$ ls -l total 48 -rw-rw-r-- 1 datasoft datasoft 729 Jul 28 15:34 abc.txt drwxr-xr-x 2 datasoft datasoft 4096 Jun 20 12:41 Desktop drwxr-xr-x 2 datasoft datasoft 4096 Jun 20 12:41 Documents drwxr-xr-x 2 datasoft datasoft 4096 Jun 20 12:41 Downloads -rw-r--r-- 1 datasoft datasoft 8980 Jun 20 11:42 examples.desktop drwxr-xr-x 2 datasoft datasoft 4096 Jun 20 12:41 Music drwxr-xr-x 2 datasoft datasoft 4096 Jun 20 12:41 Pictures drwxr-xr-x 2 datasoft datasoft 4096 Jun 20 12:41 Public drwxr-xr-x 2 datasoft datasoft 4096 Jun 20 12:41 Templates -rw-rw-r-- 1 datasoft datasoft 0 Jul 28 16:21 typescript drwxr-xr-x 2 datasoft datasoft 4096 Jun 20 12:41 Videos [email protected]:~$

The output shown in above file displays the :

  • Column-1 : File type - First character.
  • Column-2 : No of links
  • FAP : Column-1, 2nd-10th characters
  • Column-3 : File Owner (user name)
  • Column-4 : Group Owner (group name)
  • Column-5 : File size (in bytes)
  • Column-6,7 & 8 : Last modification of the file.
  • Column-9 : File Owner (user name)

ls -lh

Another frequently used ls option is -h. It shows the numbers (file sizes) in a more human readable format. Also shown below is some variation in the way you can give the options to ls. We will explain the details of the output later in this book.

[email protected]:~$ ls -l -h total 52K -rw-rw-r-- 1 datasoft datasoft 729 Jul 28 15:34 abc.txt drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Desktop drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Documents drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Downloads -rw-r--r-- 1 datasoft datasoft 8.8K Jun 20 11:42 examples.desktop drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Music drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Pictures drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Public drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Templates -rw-rw-r-- 1 datasoft datasoft 4.0K Jul 28 16:46 typescript drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Videos [email protected]:~$ ls -lh total 52K -rw-rw-r-- 1 datasoft datasoft 729 Jul 28 15:34 abc.txt drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Desktop drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Documents drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Downloads -rw-r--r-- 1 datasoft datasoft 8.8K Jun 20 11:42 examples.desktop drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Music drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Pictures drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Public drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Templates -rw-rw-r-- 1 datasoft datasoft 4.0K Jul 28 16:46 typescript drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Videos [email protected]:~$ ls -hl total 52K -rw-rw-r-- 1 datasoft datasoft 729 Jul 28 15:34 abc.txt drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Desktop drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Documents drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Downloads -rw-r--r-- 1 datasoft datasoft 8.8K Jun 20 11:42 examples.desktop drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Music drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Pictures drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Public drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Templates -rw-rw-r-- 1 datasoft datasoft 4.0K Jul 28 16:46 typescript drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Videos [email protected]:~$ ls -h -l total 52K -rw-rw-r-- 1 datasoft datasoft 729 Jul 28 15:34 abc.txt drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Desktop drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Documents drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Downloads -rw-r--r-- 1 datasoft datasoft 8.8K Jun 20 11:42 examples.desktop drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Music drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Pictures drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Public drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Templates -rw-rw-r-- 1 datasoft datasoft 4.0K Jul 28 16:46 typescript drwxr-xr-x 2 datasoft datasoft 4.0K Jun 20 12:41 Videos [email protected]:~$

mkdir

mkdir command is used to create a new dir. You have to give at least one parameter to mkdir, the name of the new directory to be created. Think before you type a leading / .

[email protected]:~/MyDir$ mkdir test [email protected]:~/MyDir$ cd test [email protected]:~/MyDir/test$ ls -al total 8 drwxrwxr-x 2 datasoft datasoft 4096 Jul 28 16:51 . drwxrwxr-x 5 datasoft datasoft 4096 Jul 28 16:51 .. [email protected]:~/MyDir/test$ mkdir staff [email protected]:~/MyDir/test$ mkdir casualstaff [email protected]:~/MyDir/test$ ls -l total 8 drwxrwxr-x 2 datasoft datasoft 4096 Jul 28 16:53 casualstaff drwxrwxr-x 2 datasoft datasoft 4096 Jul 28 16:52 staff [email protected]:~/MyDir/test$

mkdir -p

When given the option -p, then mkdir will create parent directories as needed.

[email protected]:~$ mkdir -p test3/Mytest/sample [email protected]:~$ ls test3 Mytest [email protected]:~$ ls test3/Mytest sample [email protected]:~$ ls test3/Mytest/sample [email protected]:~$

rmdir

rmdir is used to remove an empty directory.

[email protected]:~$ rmdir test3/Mytest/sample [email protected]:~$ ls abc.txt Documents examples.desktop MyDir Public test3 Videos Desktop Downloads Music Pictures Templates typescript [email protected]:~$ cd .. [email protected]:~$ rmdir test3/Mytest/sample [email protected]:~$

rmdir -p

And similar to the mkdir -p option, you can also use rmdir to recursively remove directories.

[email protected]:~$ mkdir -p dir/subdir/subdir2 [email protected]:~$ rmdir -p dir/subdir/subdir2 [email protected]:~$

Exercise and Solution:

1. Display your current directory.

pwd

2. Change to the /etc directory.

Code:

cd /etc

3. Now change to your home directory using only three key presses.

Code:

cd (and the enter key)

4. Change to the /boot/grub directory using only eleven key presses.

Code:

cd /boot/grub (use the tab key)

5. Go to the parent directory of the current directory.

Code:

cd .. (with space between cd and ..)

6. Go to the root directory.

Code:

cd /

7. List the contents of the root directory.

Code:

ls

8. List a long listing of the root directory.

Code:

ls -l/

9. Stay where you are, and list the contents of /etc.

Code:

ls /etc

10. Stay where you are, and list the contents of /bin and /sbin.

Code:

ls /bin /sbin

11. Stay where you are, and list the contents of ~.

Code:

ls ~

12. List all the files (including hidden files) in your home directory.

Code:

ls -al ~

13. List the files in /boot in a human readable format.

Code:

ls -lh /boot

14. Create a directory testdir in your home directory.

Code:

mkdir ~/testdir

15. Change to the /etc directory, stay here and create a directory newdir in your home directory.

Code:

cd /etc ; mkdir ~/newdir

16. Create in one command the directories ~/dir1/dir2/dir3 (dir3 is a subdirectory from dir2,
and dir2 is a subdirectory from dir1 ).

Code:

mkdir -p ~/dir1/dir2/dir3

17. Remove the directory testdir.

Code:

rmdir testdir

18. If time permits (or if you are waiting for other students to finish this practice), use and understand pushd and popd. Use the man page of bash to find information about these commands.

Code:

man bash

Previous: Linux - Control-operators
Next: Linux - Working with files

Which command is used to list all the File in your current directory Mcq?

Explanation: ls command when used with directory name as an argument, lists all the files in the directory.

Which command will list all files including all hidden files?

The ls command lists the contents of the current directory. The –a switch lists all files – including hidden files.

Which command is used to display hidden files in current directory?

Using the command line command dir /ah displays the files with the Hidden attribute. In addition, there is a System file attribute that can be set on a file, which also causes the file to be hidden in directory listings. Use the command line command dir /as to display the files with the System attribute.

Which command is used to list out all the hidden files along with other files in Unix?

You need to use the find command to list all hidden files recursively on a Linux or Unix like systems. You can also use the ls command to list hidden files.