ls For Beginners
Tux le manchot, mascotte Linux dessiné par Larry Ewing

ls For Beginners

The? ls command lists files and directories

  • tl;dr: <ls>? lists the content of directories; <ls *.c> lists all files ending by .c

It exists on all Unix systems, and it’s more or less equivalent to dir on Windows systems.

Funnily enough, "dir" also exists on Linux where "info dir" shows :

'dir' is equivalent to ‘ls -C -b’; that is, by default files are listed in columns, sorted vertically, and special characters are represented by backslash escape sequences.

On Linux, ls is part of the GNU coreutils; as such, the source code is available at: https://github.com/wertarbyte/coreutils/blob/master/src/ls.c

For example, checking the package on Ubuntu confirms it is part of GNU coreutils :

$> dpkg -S /bin/ls 
coreutils: /bin/ls        

Checkout the manual (man page) of the ls command here: https://www.man7.org/linux/man-pages/man1/ls.1.html

Be aware of the fact that ls (most of the time) is an alias; test it out in your terminal, type?

$> type ls
ls is aliased to `ls --color=auto'        

Here is an example of what ls does:? ls *.c

$> ls *.c
101-quote.c? 4-puts.c? 5-printf.c? 6-size.c        

The "*" is a wildcard that matches “everything” to see the full extent of what it can do: https://linuxhint.com/bash_wildcard_tutorial/?

As displayed above, only the files whose names end with ".c" are printed. Keep in mind that once the command goes through, Bash will print the prompt again.

You can use shell expansions (for example, "~" which is for the current user home directory) to specify the exact path for ls

$> ls ~/school
/root/school        

By default, ls is found because the command is located in a directory listed in the environment variable PATH.??

$> unalias ls
$> type ls
ls is hashed (/usr/bin/ls)
$> echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin        

Remember that existing aliases are always used before the actual commands.

?When it exists, an alias is the one being used rather than a built-in, a function, or a command with the same name.?

You can customize the shell prompt using the environment variable PS1

PS1 can be very complex if you purposely make it this way.?Go see the examples shared by Linux config: https://linuxconfig.org/bash-prompt-basics

Assignment completed, Mission accomplished!

要查看或添加评论,请登录

Chloé C.的更多文章

社区洞察

其他会员也浏览了