With the help of the ls command, users and superusers list information about files and directories. This article helps the administrator and user identify file permissions, which is useful for access and basic troubleshooting.
Using ls
At its most basic, you can use the ls command to see what files are in a directory: Note: The listing may or may not be color-coded.
[root@centos7 ~]# ls /etc
abrt firewalld makedumpfile.conf.sample rdma
adjtime flatpak man_db.conf redhat-release
aliases fonts maven request-key.conf
aliases.db fprintd.conf mime.types request-key.d
alsa fstab mke2fs.conf resolv.conf
alternatives fuse.conf modprobe.d rhsm
anacrontab gconf modules-load.d rpc
...
There are many flags available to give you more information with your file listing. The most common one to use is -a (or --all), since it shows you all of the files in the directory, including the ones that start with a dot (e.g., .bash_logout).
Another common flag is -l, which gives you more information about the files in a directory, including the file’s permissions, ownership, and size. If you want to be able to tell more easily what units a file size is in (e.g., 1K, 234M, 2G), you can add the -h (short for --human-readable) flag.
Let’s walk through an example. A simple file listing in the root user’s directory might be this:
[root@name ~]# ls /root
anaconda-ks.cfg
If you want the long format version of this list (we will cover the additional information shown here in the next section):
[root@name ~]# ls -l /root
total 4
-rw-------. 1 root root 896 Feb 22 01:40 anaconda-ks.cfg
However, if you add the -a flag, the ls command returns a short-form list including the hidden items:
[root@name ~]# ls -a /root
. anaconda-ks.cfg .bash_logout .bashrc .cshrc .tcshrc
.. .bash_history .bash_profile .cache .local
Consider the single period and double period in both directory lists. The single dot (.) refers to the directory itself. This dot is convenient if you want to run a command and reference your current directory (for example, when you want to copy a file there).
The double dots (..) refer to the parent directory. If you type cd .. you move up one step in the filesystem hierarchy. For example, if your current directory is /root, typing cd .. would take you to /, the very top of the hierarchy.
If you combine the -a option with the -l option into -la, you get more details about the hidden and not hidden files:
[root@name ~]# ls -la /root
total 44
dr-xr-x---. 4 root root 4096 Feb 11 05:47 .
dr-xr-xr-x. 19 root root 4096 Jul 11 13:15 ..
-rw-------. 1 root root 896 Feb 22 01:40 anaconda-ks.cfg
-rw-------. 1 root root 163 Jul 11 01:18 .bash_history
-rw-r--r--. 1 root root 18 Feb 2 10:37 .bash_logout
-rw-r--r--. 1 root root 176 Feb 2 10:37 .bash_profile
-rw-r--r--. 1 root root 176 Feb 2 10:37 .bashrc
drwx------. 3 root root 4096 Jul 11 01:37 .cache
-rw-r--r--. 1 root root 100 Feb 2 10:37 .cshrc
drwxr-xr-x. 3 root root 4096 Feb 22 01:57 .local
-rw-r--r--. 1 root root 129 Feb 2 10:37 .tcshrc
Decoding the long listing format
When you start using the -l flag, you will notice a lot more information. Here is an example line of output:
drwxrwxr-x 2 root test 4096 Dec 3 2009 test
The first character to the left tells you what you’re looking at. If it’s a d, as you see here, that tells that this file is a directory, which is basically a special kind of file (and it will be referred to as a file throughout). However, if it was a dash (-), that would indicate that the file is a regular file.
If the first character was a lowercase l, then this item would be a special file type called a symlink, or soft link, which is a pointer (shortcut) to another location in the filesystem. Such a listing might look like this:
lrwxrwxrwx 1 root root 4 Jun 30 03:29 sh -> bash
After the first character are the permission abbreviations, which are grouped in threes. Permissions for files are represented by the following letters:
rfor the read permissionwfor the write permissionxfor the execute permission
Back to this example:
drwxrwxr-x 2 root test 4096 Dec 3 2009 test
- The first triplet (
rwx) shows the permissions for the user or file owner. - The next triplet (
rwx) shows the permissions for the group category. - The last triplet (
r-x) shows the permissions for the final category, other.
In this example, users who are neither the file owner nor in the group have read and execute permissions but not write, as indicated by the dash (-) in the middle position.
The number listed after the permissions indicates either the link count (for a file) or the number of contained directory entries (for a directory). This number is not relevant for permissions.
After this number, the first name indicates the file’s owner. The user permissions (first triplet) apply to this owner, so in this example, the user root has read, write, and execute permissions for this directory.
The second name is the file’s group. The group permissions apply to any user in the same group, so in this case, anyone in the mail group has read, write, and execute permissions.
Wrapping up
Now you should have a much better feeling for getting information about your files from the command line. The ls command has many more options. Type man ls to learn more, and figure out your favorite flag combinations for getting things done. You might even want to make some aliases.
저자 소개
유사한 검색 결과
More than meets the eye: Behind the scenes of Red Hat Enterprise Linux 10 (Part 4)
Why should your organization standardize on Red Hat Enterprise Linux today?
The Overlooked Operating System | Compiler: Stack/Unstuck
Linux, Shadowman, And Open Source Spirit | Compiler
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
가상화
온프레미스와 클라우드 환경에서 워크로드를 유연하게 운영하기 위한 엔터프라이즈 가상화의 미래