الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

ls

List the files in a folder, with details like permissions, owner, size and time.

المستوى
امتحان RHCSA
المجموعة
الملفات والمجلدات
يحتاج root
لا
الحزمة
coreutils

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

ls shows what is in a folder (the current one when you give no name). With -l you get the long list:

-rw-r--r--. 1 root root 158 Jan  6 10:12 /etc/hosts

From left to right: file type and permissions (the . at the end means an SELinux context exists), number of hard links, owner, group, size in bytes, last change time, name.

The first letter is the type: - file, d folder, l symbolic link, b block device, c character device, p pipe, s socket.

On RHEL, ls is an alias for ls --color=auto, and ll for ls -l --color=auto.

طريقة الكتابة

ls [OPTION]... [FILE]...

الخيارات 60

الأكثر استخدامًا

What to show

-a, --all
Also show hidden files (names that start with a dot), and . and ...
-A, --almost-all
Show hidden files, but not . and ...
-d, --directory
Show the folder itself, not its content.
-R, --recursive
Also list all sub-folders.
-B, --ignore-backups
Do not list backup files that end with ~.
-I, --ignore=pattern
Do not list names that match the pattern.
--hide=pattern
Like -I, but -a or -A show them again.
-L, --dereference
For links, show the file the link points to.
-H, --dereference-command-line
Follow links given on the command line.

Long list and details

-l
Long list: permissions, links, owner, group, size, time, name.
-h, --human-readable
With -l: sizes like 4.0K, 23M, 1.2G.
--si
Like -h, but with powers of 1000 instead of 1024.
-i, --inode
Show the inode number of each file.
-n, --numeric-uid-gid
Like -l, but show user and group IDs as numbers.
-g
Like -l, but without the owner.
-o
Like -l, but without the group.
-G, --no-group
In a long list, do not show the group.
-s, --size
Show the space each file uses on disk (in blocks).
--block-size=size
Show sizes in this unit, like M or G.
-k, --kibibytes
Use 1024-byte blocks for the disk use.
-Z, --context
Show the SELinux security context.
--author
With -l: show the author of each file.
--full-time
Like -l with the full date and time.
--time-style=style
Time format: full-iso, long-iso, iso, locale or +FORMAT.

Sort

-t
Sort by time, newest first.
-r, --reverse
Reverse the order. With -t: oldest first.
-S
Sort by size, biggest first.
-X
Sort by the file extension.
-v
Natural sort of version numbers (file2 before file10).
-U
Do not sort; show in the order of the folder.
-f
Do not sort, show all files (like -aU).
--sort=word
Sort by: none, size, time, version, extension, width.
--group-directories-first
Show folders before files.
-c
With -lt: sort by and show the change time (ctime) of the file status.
-u
With -lt: sort by and show the access time.
--time=word
Which time to show and sort by: atime, ctime, mtime, birth.

Format

-1
One name per line.
-C
Names in columns (the default on a terminal).
-x
Names in rows instead of columns.
-m
Names separated by commas.
--format=word
across, commas, horizontal, long, single-column, verbose, vertical.
-w, --width=cols
Set the screen width.
-T, --tabsize=cols
Tab stops every COLS columns.
-F, --classify[=<when>]
Add a sign to the name: / folder, * program, @ link, | pipe, = socket.
--file-type
Like -F, but no * for programs.
-p, --indicator-style=slash
Add / to folder names.
--indicator-style=word
none, slash, file-type or classify.
--color[=<when>]
Colors: always, auto, never.
-b, --escape
Show special characters as C escapes.
-N, --literal
Show names exactly as they are.
-q, --hide-control-chars
Show control characters as ?.
--show-control-chars
Show control characters as they are.
-Q, --quote-name
Put names in double quotes.
--quoting-style=word
How to quote names: literal, shell, shell-always, c, escape …
-D, --dired
Output for the Emacs dired mode.
--zero
End each line with a NUL byte.
--help
Show help.
--version
Show the version.

أمثلة 13

$ مستخدم عادي# بصلاحية root (أو مع sudo)

  1. 1Long list with readable sizes

    ls -lh /var/log

    The basic view to check owners, permissions and sizes.

  2. 2Show hidden files

    ls -la ~

    Files like .bashrc and .ssh are hidden. -a shows them.

  3. 3What changed last?

    ls -lrt /etc | tail

    Sorted by time, the newest files at the bottom. Great after an update or an incident.

  4. 4Show the folder itself, not its content

    ls -ld /tmp /etc
    drwxrwxrwt. 18 root root 4096 Oct 25 09:12 /tmp
    drwxr-xr-x. 140 root root 8192 Oct 25 08:55 /etc

    Check the permissions of a folder. The t on /tmp is the sticky bit.

  5. 5Show the SELinux context

    ls -Z /var/www/html

    The label must be httpd_sys_content_t for Apache. Wrong labels cause "403 Forbidden".

  6. 6Find the biggest files in a folder

    ls -lS /var/log | head

    Biggest first. For a whole tree use du or find -size.

  7. 7Show inode numbers to see hard links

    ls -li /data/projects/web/hosts.old /data/projects/db/hosts.hard

    The same inode number means the same data.

  8. 8List only folders

    ls -d /etc/*/

    The slash at the end of the pattern matches folders only.

  9. 9Show user and group as numbers

    ls -ln /home

    Useful after a restore or on NFS, when names do not match on two systems.

  10. 10Exact times in ISO format for a report

    ls -l --time-style=long-iso /etc/passwd /etc/shadow

    When was the account database last changed? A good question in a security check.

  11. 11Show which files are links and programs

    ls -F /usr/local/bin

    @ = link, * = program, / = folder.

  12. 12One name per line for a script

    ls -1 /etc/yum.repos.d/

    Better: use a for f in /etc/yum.repos.d/* loop or find in scripts; never parse ls -l.

  13. 13Folders first, then files, sorted naturally

    ls -l --group-directories-first -v /srv/releases

    Version numbers like v2, v10 are sorted in the right order.

انتبه

  • Do not parse the output of ls in scripts (names with spaces or new lines break it). Use globs or find -print0.
  • The size in ls -l is the file length, not the space used on disk. ls -s or du shows the real use.
  • ls -l of a link shows the link; add -L to see the target file.

نصائح للحفظ

  • -l long, -a all, -h human, -t time, -r reverse, -d the folder itself, -Z SELinux.
  • ls -lrt = newest at the bottom; ls -lS = biggest first.

الدليل الكامل

هذه الصفحة تشرح الأمر بكلمات بسيطة. الدليل الأصلي فيه كل التفاصيل.

في الطرفية: man ls