Dir command in Linux with examples
With the help of this command, we can find the properties of file such as file size, permissions, modified date, etc. Use the following command line to list the files and directories 6. This implies that a directory is also a file which contains some list of files and subdirectories.
Basic Shell Commands in Linux
A shell is a special user program that provides an interface to the user to use operating system services. Shell accepts human-readable commands from the user and converts them into something which the kernel can understand. It is a command language interpreter that executes commands read from input devices such as keyboards or from files. The shell gets started when the user logs in or starts the terminal.
1). Displaying the file contents on the terminal:
- cat : It is generally used to concatenate the files. It gives the output on the standard output.
- more : It is a filter for paging through text one screenful at a time.
- less : It is used to viewing the files instead of opening the file.Similar to more command but it allows backward as well as forward movement.
- head : Used to print the first N lines of a file. It accepts N as input and the default value of N is 10.
- tail : Used to print the last N-1 lines of a file. It accepts N as input and the default value of N is 10.
2). File and Directory Manipulation Commands:
- mkdir : Used to create a directory if not already exist. It accepts the directory name as an input parameter.
- cp : This command will copy the files and directories from the source path to the destination path. It can copy a file/directory with the new name to the destination path. It accepts the source file/directory and destination file/directory.
- mv : Used to move the files or directories. This command’s working is almost similar to cp command but it deletes a copy of the file or directory from the source path.
3). Extract, sort , and filter data Commands:
- grep with Regular Expressions : Used to search for text using specific regular expressions in file.
4). Basic Terminal Navigation Commands:
- ls : To get the list of all the files or folders.
- ls -l: Optional flags are added to ls to modify default behavior, listing contents in extended form -l is used for “long” output
- ls -a: Lists of all files including the hidden files, add -a flag
- cd : Used to change the directory.
- du : Show disk usage.
- pwd : Show the present working directory.
- man : Used to show the manual of any command present in Linux.
- rmdir : It is used to delete a directory if it is empty.
- lnfile1 file2 : Creates a physical link.
- ln-s file1 file2 : Creates a symbolic link.
- locate: It is used to locate a file in Linux System
- echo: This command helps us move some data, usually text into a file.
- df: It is used to see the available disk space in each of the partitions in your system.
- tar: Used to work with tarballs (or files compressed in a tarball archive)
5). File Permissions Commands: The chmod and chown commands are used to control access to files in UNIX and Linux systems.
- chown : Used to change the owner of the file.
- chgrp : Used to change the group owner of the file.
- chmod : Used to modify the access/permission of a user.
Basic Shell Commands in Linux, Displaying the file contents on the …File and Directory Manipulation …Extract, sort, and filter data …Basic Terminal Navigation …File Permissions Commands: The …
Dir command in Linux with examples
dir command in Linux is used to list the contents of a directory .
How is dir command different from ls?
dir command differs from ls command in the format of listing contents that is in default listing options .
By default, dir command lists the files and folders in columns, sorted vertically and special characters are represented by backslash escape sequences. But unlike ls, when the output is on terminal, it does not produce colored output as ls does.
Useful options for dir command
1. -a or –all option: It displays all the hidden files(starting with `.`) along with two files denoted by `.` and `..` which signals for current and previous directory respectively.
SYNTAX:
EXAMPLE:
2. -A or –almost-all option: It is similar to -a option except that it does not display files that signals the current directory and previous directory.
SYNTAX:
EXAMPLE:
3. -l –author option: Displays author of all the files. -l is required to display the contents in the form of a list.
SYNTAX:
EXAMPLE:
4. -B or –ignore-backups option: Ignores listing of backed up files. These files end with a `~`.
SYNTAX:
5. –color option: (can be followed by =[TIME]).It is used to colorize the output. In the absence of time specification which can be auto, never or always, it colorizes the output always (default behavior).
SYNTAX:
EXAMPLE:
6. -F, –classify option: Append indicator (one of */=>@|) to the file names which classifies them into their typ
- A slash (`/`) indicates a directory.
- An asterisk (`*`) indicates an executable.
- An at sign (`@`) indicates a symbolic link.
- A percent sign (`%`) indicates a whiteout.
- An equal sign (`=`) indicates a socket.
- A vertical bar (`|`) indicates a FIFO.
EXAMPLE:
7. –file-type option: It is same as -F option, except that it does not append `*` to the executables.
SYNTAX:
EXAMPLE:
8. –format=WORD option: It formats the listing of entries. The WORD can take the following values: across, commas, horizontal, long, single-column, verbose, vertical. The same can be achieved by passing -x, -m, -x, -l, -1, -l, -C options to dir command for each of the respective values.
SYNTAX:
EXAMPLE 1:
EXAMPLE 2:
9. –hide=PATTERN or –ignore=PATTERN option: It ignores files described by shell PATTERN while listing the Contents Of A Directory .
SYNTAX:
EXAMPLE:
10. -n, –numeric-uid-gid option: This option is similar to the long listing that is -l option except that it lists numeric user and group IDs.
SYNTAX:
11. -r, –reverse option: list files in reverse order whi
orting.
SYNTAX:
EXAMPLE:
12.-R, –recursive option: List subdirectories recursively.
SYNTAX:
EXAMPLE:
13.–sort=PARAMETER: To List files in a sorted manner described by the PARAMETER. The PARAMETER can take the following values: none (-U), size (-S), time (-t), version (-v), extension (-X).Instead of passing –sort option, the flags indicated in the brackets can directly be passed as options to sort the listing.
SYNTAX:
EXAMPLE 1:
EXAMPLE 2:
14. –help option: Display the help options and exit.
SYNTAX:
EXAMPLE:
15. –version option: Outputs version information and exit.
SYNTAX:
Change directory commands in linux Code Example, # Few shorctus for changing directory # go home directory cd ~ # go to root directory cd / # go back to previous directory cd — # go up one directory level cd .. # go up two directory level cd ../../ # and so on
Linux List Directories
Introduction to Linux List Directories
We know that the Linux system is made up of files and directories. At any stage, while working on the Linux command mode you may want to list all files and subdirectories in the directory. There are many ways to list the contents of the directories in our Linux system. In this article, we will discuss about what are the command and options that are used to List Directories in the Linux system.
Commands Used to List Directories
There are many commands used to list files and Directories in the Linux system. Let us discuss them one by one.
1. ls Command
The ls command is the basic command used to list files and directories within the Linux file system. But if you want to list only directories, ls command offers some options. Let us discuss them with examples.
How to List Directories Using Wildcards
The simplest method to list directories is using wildcards.
To list directories in the Long listing format use the -l option.
Using grep and -f option
The -F option adds a trailing forward slash. This option allows us to grep directories by ‘grep’ing lines that ends with forward slash.
using grep and -l option
While using the long listing option –l we can grep the lines that starts with d. we can print only the directory names by using the following command.
2. Using dir Command
The dir command is used to list the contents of the directory . The syntax is as follows.
The forward slash in the above output indicates the directory and others indicate files. To print the detailed information of each file and directory use the -l command-line option.
In the above output, d indicates directories.
3. Using printf Command
This command is used for formatting and printing text. It will display the given parameters in the specified format or execute the command depending on the option specified. To display the contents of current working directory run the following command.
The output will display both files and directories.
4. Using find Command
The find command helps us to look up for files for which we know only the approximate names.In simple words the find command searches for a file in the current working directory and recursively through the subdirectories that matches the given search criteria. You can search files by name, extension, group, modification date, permissions etc.
To display all files which are present in the current working directory, use the following command.
The dot (.) symbol indicates the current working directory.
cd documents/
~/documents$ find .
How to List only Directories Using Find Command
If you want to search only the directories and skip the file names use the -type d option as shown below.
5. Listing Directories Using Stat Command
This command is used to display the information of files and filesystem. With the help of this command, we can find the properties of file such as file size, permissions, modified date, etc.
Use the following command line to list the files and directories
6. Listing Directories Using Grep Command
This command is used for searching text files using regular expressions. To list the contents of the directory using grep command run the following command.
7. Listing Directories Using Lsattr Command
To list the files and directories in the current directory use the following command.
This command displays only the contents of the directory just down one level which means it cannot display other subdirectories and its contents and also hidden files.
8. Listing Directories Using Getfacl Command
The getfacl and setfacl are two of the mostt important and useful commands every system administrator should know to set the Access Control Lists of the files and directories.
To display the files and directories in the current working directory, just run the following command.
The key features of this command is that it not only lists the contents of the directory but also other details such as
- Owner of the file or directory
- All the group names which has access to the file or directory
- Access rights to group, owner and others.
9. Listing Directories Using for Loop
This is another method to list the contents of the directory. Use the following command shown below.
10. Listing Directories Using Vim Editor
In addition to viewing and editing files , vim editor can also be used for listing files and directories. We already know that everything is considered as a file in Linux. This implies that a directory is also a file which contains some list of files and subdirectories.
To list the files and directories in the current working directory using vim editor, run the command shown below.
The dot (.) at the end of the command implies the current working directory. Inorder to list the information of a specific directory, run the following command.
For Example:
We can use the up and down keys to navigate through the screen. To list the contents of the subdirectory, place the cursor on the subdirectory and then press ENTER key.
11. Listing Directories Using Tree Command
This command is used to list the directory contents in a tree like format.
The above command will list the files and subdirectories of the current directory. To list the directory contents down one level, run the following command.
Final thoughts
This is a guide to Linux List Directories. Here we also discuss the Introduction and commands used to list directories along with different examples and its code implementation. You may also have a look at the following articles to learn more –
Linux commands to go back a directory Code Example, “linux commands to go back a directory” Code Answer. python print sql in shell_plus; bash «=~» example; bash command to open file explorer; bash substract varible; bash alias function that accepts arguments; vi quit; vim quit; vi quit and save; code command line options; interesting bash scripts;