Linux Shell Scripting with Bash (Listing Files)

lsandrmcommand

Listing Files
The ls (list) command shows the contents of the current directory.

$ ls
archive command_checker.sh commands.txt

ls has switches that affect how the files are listed, including the level of detail, the sorting order, and the number of columns. ls hides files that begin with a period.This is the Linux convention for configuration files, history files, and other files that a user isn’t normally interested in.

To see these files,use the -

A (all) switch. Use -a (absolutely all) to show the implicit . and .. files as well.

 $ ls -A
.bash_history .bash_logout .bash_profile .bashrc archive
command_checker.sh commands.txt

The filenames can be printed in color to show the kind of file they are. The colors are defined in a file /etc/DIR_COLORS. You can customize the colors using a .dir_colors file in your own directory. The format of the file is described in the
/etc/DIR_COLORS file.

To display the files without color and with symbols instead, use the –color and –classify (or -F) switches. (On most Linux distributions, this feature is turned on using aliases.)

$ ls --color=never --classify
archive/ command_checker.sh* commands.txt

The –classify symbols are directories (/), programs (*), symbolic links (@), pipes (|), and Unix domain socket files (=). These symbols are not a part of the name: They are hints as to the type of file. In this example, archive is a directory and command_checker.sh is a program.

Another very important switch is –hide-control-chars (or -q). Linux filenames can contain any character, even control characters. It is possible to create a filename with hidden characters in the name. In these cases, you can’t rename or delete the file unless you know what the hidden characters are. Contrary to what the name implies, the
–hide-control-chars switch displays any unprintable characters in the filename as question marks, making their locations visible.

 $ rm commands.txt 
rm: commands.txt  non-existent
$ ls --color=never --classify –-hide-control-chars
archive/ command_checker.sh* comm?ands.txt
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s