Sie sind auf Seite 1von 27

ls command in Linux/Unix

ls is a Linux shell command that lists directory contents of files and directories.
 ls syntax
 ls options
 ls examples
 ls code generator

ls syntax
$ ls [options] [file|dir]

ls command options
ls command main options:
option description
ls -a list all files including hidden file starting with '.'
ls --color colored list [=always/never/auto]
ls -d list directories - with ' */'
ls -F add one char of */=>@| to enteries
ls -i list file's inode index number
ls -l list with long format - show permissions
ls -la list long format including hidden files
ls -lh list long format with readable file size
ls -ls list with long format with file size
ls -r list in reverse order
ls -R list recursively directory tree
ls -s list file size
ls -S sort by file size
ls -t sort by time & date
ls -X sort by extension name

ls command examples
You can press the tab button to auto complete the file or folder names.
List directory Documents/Books with relative path:
$ ls Documents/Books

List directory /home/user/Documents/Books with absolute path.


$ ls /home/user/Documents/Books

List root directory:


$ ls /

List parent directory:


$ ls ..

List user's home directory (e.g: /home/user):


$ ls ~

List with long format:


$ ls -l

Show hidden files:


$ ls -a

List with long format and show hidden files:


$ ls -la
Sort by date/time:
$ ls -t

Sort by file size:


$ ls -S

List all subdirectories:


$ ls *

Recursive directory tree list:


$ ls -R

List only text files with wildcard:


$ ls *.txt

ls redirection to output file:


$ ls > out.txt

List directories only:


$ ls -d */

List files and directories with full path:


$ ls -d $PWD/*

cat command in Linux/Unix


Linux cat command.
cat command is used to display the content of text files and to combine several files to
one file.
The cat command does not accept directories.

cat command syntax


$ cat [options] file1 [file2...]

cat command options


cat command main options:
option description
cat -b add line numbers to non blank lines
cat -n add line numbers to all lines
cat -s squeeze blank lines to one line
cat -E show $ at the end of line
cat -T show ^I instead of tabs

cat command examples


View text file data:
$ cat list1.txt
milk
bread
apples

$ cat list2.txt
house
car

Combine 2 text files:


$ cat list1.txt list2.txt
milk
bread
apples

house
car

Combine 2 text files to another file:


$ cat list1.txt list2.txt > todo.txt
$

cd command in Linux/Unix
cd is a Linux command to change the directory/folder of the terminal's shell.
You can press the tab button in order to auto complete the directory name.
 cd syntax
 cd examples

cd syntax
$ cd [directory]

cd command examples
Change to home directory (determined by $HOME environment variable):
$ cd

Also change to home directory:


$ cd ~

Change to root directory:


$ cd /

Change to parent directory:


$ cd ..

Change to subdirectory Documents:


$ cd Documents

Change to subdirectory Documents/Books:


$ cd Documents/Books

Change to directory with absolute path /home/user/Desktop:


$ cd /home/user/Desktop

Change to directory name with white space - My Images:


$ cd My\ Images
Or
$ cd "My Images"
Or
$ cd 'My Images'
cp command in Linux/Unix
cp is a Linux shell command to copy files and directories.
 cp syntax
 cp options
 cp examples
 cp code generator

cp command syntax
Copy from source to dest
$ cp [options] source dest

cp command options
cp command main options:
option description

cp -a archive files

cp -f force copy by removing the destination file if needed

cp -i interactive - ask before overwrite

cp -l link files instead of copy

cp -L follow symbolic links

cp -n no file overwrite

cp -R recursive copy (including hidden files)

cp -u update - copy when source is newer than dest

cp -v verbose - print informative messages

cp command examples
Copy single file main.c to destination directory bak:
$ cp main.c bak

Copy 2 files main.c and def.h to destination absolute path directory /home/usr/rapid/ :
$ cp main.c def.h /home/usr/rapid/

Copy all C files in current directory to subdirectory bak :


$ cp *.c bak

Copy directory src to absolute path directory /home/usr/rapid/ :


$ cp src /home/usr/rapid/

Copy all files and directories in dev recursively to subdirectory bak:


$ cp -R dev bak

Force file copy:


$ cp -f test.c bak

Interactive prompt before file overwrite:


$ cp -i test.c bak
cp: overwrite 'bak/test.c'? y

Update all files in current directory - copy only newer files to destination directory bak:
$ cp -u * bak

cp code generator

GCC C Compiler
GCC is a short of GNU Compiler Collection, a C compiler for Linux.
 gcc syntax
 gcc options
 gcc examples
 gcc code generator

GCC syntax
$ gcc [options] [source files] [object files] [-o output file]

GCC options
GCC main options:
option description
gcc -c compile source files to object files without linking
gcc -Dname[=value] define a preprocessor macro
gcc -fPIC generate position independent code for shared libraries
gcc -glevel generate debug information to be used by GDB
gcc -Idir add include directory of header files
gcc -llib link with library file
gcc -Ldir look in directory for library files
gcc -o output
file write build output to output file
gcc -Olevel optimize for code size and execution time
gcc -shared generate shared object file for shared library
gcc -Uname undefine a preprocessor macro
gcc -w disable all warning messages
gcc -Wall enable all warning messages
gcc -Wextra enable extra warning messages

GCC examples
Compile file1.c and file2.c and link to output file execfile:
$ gcc file1.c file2.c -o execfile

Run output file execfile:


$ ./execfile

Compile file1.c and file2.c without linking:


$ gcc -c file1.c file2.c

Compile myfile.c with debug information and link to output file execfile:
$ gcc -g myfile.c -o execfile

Compile myfile.c with warning messages enabled and link to output file execfile:
$ gcc -Wall myfile.c -o execfile

Compile myfile.c with and link with static library libmath.a located in /user/local/math to
output file execfile:
$ gcc -static myfile.c -L/user/local/math -lmath -o execfile

Compile myfile.c with optimization and link to output file execfile:


$ gcc -O myfile.c -o execfile

mv command in Linux/Unix
Linux mv command.
mv command is used to move files and directories.

mv command syntax
$ mv [options] source dest

mv command options
mv command main options:
option description
mv -f force move by overwriting destination file without prompt
mv -i interactive prompt before overwrite
mv -u update - move when source is newer than destination
mv -v verbose - print source and destination files
man mv help manual

mv command examples
Move main.c def.h files to /home/usr/rapid/ directory:
$ mv main.c def.h /home/usr/rapid/

Move all C files in current directory to subdirectory bak :


$ mv *.c bak

Move all files in subdirectory bak to current directory :


$ mv bak/* .

Rename file main.c to main.bak:


$ mv main.c main.bak

Rename directory bak to bak2:


$ mv bak bak2

Update - move when main.c is newer:


$ mv -u main.c bak
$

Move main.c and prompt before overwrite bak/main.c:


$ mv -v main.c bak
'bak/main.c' -> 'bak/main.c'
$

pwd command in Linux/Unix


Unix/Linux pwd command.
pwd - print working directory, is a Linux command to get the current working directory.
 pwd syntax
 pwd examples

pwd syntax
$ pwd [option]

pwd command examples


Change directory to /usr/src directory and print working directory:
$ cd /usr/src
$ pwd
/user/src

Change directory to home directory and print working directory:


$ cd ~
$ pwd
/home/user

Change directory to parent directory of the home directory and print working directory:
$ cd ~/..
$ pwd
/home

Change directory to root directory and print working directory:


$ cd /
$ pwd
/

How to get current working directory


Unix/Linux get current working directory.
To get the current working directory use the pwd command.
For example if we change the directory to /home/user, pwd will print /home/user as the
current working directory:
$ cd /home/user
$ pwd
/home/user

In Bash shell script you can get the current working directory by:
dir=$(PWD)

pwd command ►

pwd - print working directory, is a Linux command to get the current working directory.
 pwd syntax
 pwd examples

pwd syntax
$ pwd [option]

pwd command examples


Change directory to /usr/src directory and print working directory:
$ cd /usr/src
$ pwd
/user/src

Change directory to home directory and print working directory:


$ cd ~
$ pwd
/home/user

Change directory to parent directory of the home directory and print working directory:
$ cd ~/..
$ pwd
/home

Change directory to root directory and print working directory:


$ cd /
$ pwd
/

How to view/merge text files


Linux view/combine text files.

View text file


$ cat list1.txt
milk
bread
apples

$ cat list2.txt
house
car

Combine text files


$ cat list1.txt list2.txt
milk
bread
apples
house
car

Combine 2 text files to another file


$ cat list1.txt list2.txt > todo.txt
$

Linux cat command.


cat command is used to display the content of text files and to combine several files to
one file.
The cat command does not accept directories.

cat command syntax


$ cat [options] file1 [file2...]

cat command options


cat command main options:
option description
cat -b add line numbers to non blank lines
cat -n add line numbers to all lines
cat -s squeeze blank lines to one line
cat -E show $ at the end of line
cat -T show ^I instead of tabs

cat command examples


View text file data:
$ cat list1.txt
milk
bread
apples

$ cat list2.txt
house
car

$
Combine 2 text files:
$ cat list1.txt list2.txt
milk
bread
apples

house
car

Combine 2 text files to another file:


$ cat list1.txt list2.txt > todo.txt
$

ls -a command in Linux
ls -a option flag lists all files including hidden files starting with '.'

Syntax
$ ls -a [options] [file|dir]

Examples
ls: default list:
$ ls
Desktop Downloads Pictures Templates Videos
Documents Music Public todo.txt
$

ls -a: list with hidden files/directories:


$ ls -a
. Desktop .gnome2 Music .shotwell
.. Documents .gstreamer-0.10 .nautilus Templates
.bash_logout Downloads .gtk-bookmarks Pictures
.thumbnails
.bashrc .esd_auth .gvfs .profile todo.txt
.cache .fontconfig .ICEauthority Public Videos
.config .gconf .libreoffice .pulse .xsession-
errors
.dbus .gconfd .local .pulse-cookie
$
ls -la: long listing format with hidden files:
$ ls -la
total 40
drwxr-xr-x 25 user user 680 2011-08-17 18:07 .
drwxr-xr-x 3 root root 60 2011-08-17 16:19 ..
-rw-r--r-- 1 user user 220 2011-08-17 16:19 .bash_logout
-rw-r--r-- 1 user user 3353 2011-08-17 16:19 .bashrc
drwx------ 7 user user 220 2011-08-17 17:58 .cache
drwx------ 9 user user 220 2011-08-17 18:02 .config
drwx------ 3 user user 60 2011-08-17 16:19 .dbus
drwxr-xr-x 2 user user 80 2011-08-17 16:52 Desktop
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Documents
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Downloads
-rw------- 1 user user 16 2011-08-17 16:20 .esd_auth
drwxr-xr-x 2 user user 100 2011-08-17 17:56 .fontconfig
drwx------ 4 user user 80 2011-08-17 16:52 .gconf
drwx------ 2 user user 60 2011-08-17 18:14 .gconfd
drwx------ 5 user user 100 2011-08-17 16:52 .gnome2
drwxr-xr-x 2 user user 60 2011-08-17 17:58 .gstreamer-0.10
-rw-r--r-- 1 user user 142 2011-08-17 16:52 .gtk-bookmarks
dr-x------ 2 user user 0 2011-08-17 16:52 .gvfs
-rw------- 1 user user 318 2011-08-17 16:52 .ICEauthority
drwxr-xr-x 3 user user 60 2011-08-17 17:56 .libreoffice
drwxr-xr-x 3 user user 60 2011-08-17 16:52 .local
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Music
drwxr-xr-x 2 user user 40 2011-08-17 16:52 .nautilus
drwxr-xr-x 2 user user 120 2011-08-17 18:14 Pictures
-rw-r--r-- 1 user user 675 2011-08-17 16:19 .profile
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Public
drwx------ 2 user user 160 2011-08-17 16:20 .pulse
-rw------- 1 user user 256 2011-08-17 16:20 .pulse-cookie
drwxr-xr-x 5 user user 100 2011-08-17 17:58 .shotwell
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Templates
drwx------ 3 user user 60 2011-08-17 18:02 .thumbnails
-rw-r--r-- 1 user user 131 2011-08-17 18:07 todo.txt
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Videos
-rw------- 1 user user 8030 2011-08-17 18:16 .xsession-errors
$

ls -l command in Linux
ls -l option flag lists with long listing format.

Syntax
$ ls -l [options] [file|dir]
Examples
ls, default list with short format:
$ ls
Desktop Downloads Pictures Templates Videos
Documents Music Public todo.txt
$

ls -l, long listing format:


$ ls -l
total 4
drwxr-xr-x 2 user user 80 2011-08-17 16:52 Desktop
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Documents
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Downloads
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Music
drwxr-xr-x 2 user user 120 2011-08-17 18:14 Pictures
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Public
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Templates
-rw-r--r-- 1 user user 131 2011-08-17 18:07 todo.txt
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Videos
$

ls -la, long listing format with hidden files:


$ ls -la
total 40
drwxr-xr-x 25 user user 680 2011-08-17 18:07 .
drwxr-xr-x 3 root root 60 2011-08-17 16:19 ..
-rw-r--r-- 1 user user 220 2011-08-17 16:19 .bash_logout
-rw-r--r-- 1 user user 3353 2011-08-17 16:19 .bashrc
drwx------ 7 user user 220 2011-08-17 17:58 .cache
drwx------ 9 user user 220 2011-08-17 18:02 .config
drwx------ 3 user user 60 2011-08-17 16:19 .dbus
drwxr-xr-x 2 user user 80 2011-08-17 16:52 Desktop
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Documents
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Downloads
-rw------- 1 user user 16 2011-08-17 16:20 .esd_auth
drwxr-xr-x 2 user user 100 2011-08-17 17:56 .fontconfig
drwx------ 4 user user 80 2011-08-17 16:52 .gconf
drwx------ 2 user user 60 2011-08-17 18:14 .gconfd
drwx------ 5 user user 100 2011-08-17 16:52 .gnome2
drwxr-xr-x 2 user user 60 2011-08-17 17:58 .gstreamer-0.10
-rw-r--r-- 1 user user 142 2011-08-17 16:52 .gtk-bookmarks
dr-x------ 2 user user 0 2011-08-17 16:52 .gvfs
-rw------- 1 user user 318 2011-08-17 16:52 .ICEauthority
drwxr-xr-x 3 user user 60 2011-08-17 17:56 .libreoffice
drwxr-xr-x 3 user user 60 2011-08-17 16:52 .local
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Music
drwxr-xr-x 2 user user 40 2011-08-17 16:52 .nautilus
drwxr-xr-x 2 user user 120 2011-08-17 18:14 Pictures
-rw-r--r-- 1 user user 675 2011-08-17 16:19 .profile
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Public
drwx------ 2 user user 160 2011-08-17 16:20 .pulse
-rw------- 1 user user 256 2011-08-17 16:20 .pulse-cookie
drwxr-xr-x 5 user user 100 2011-08-17 17:58 .shotwell
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Templates
drwx------ 3 user user 60 2011-08-17 18:02 .thumbnails
-rw-r--r-- 1 user user 131 2011-08-17 18:07 todo.txt
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Videos
-rw------- 1 user user 8030 2011-08-17 18:16 .xsession-errors
$

ls -lh, long listing format with file size in human readable form:
$ ls -lh
total 4
drwxr-xr-x 2 user user 80 2011-08-17 16:52 Desktop
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Documents
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Downloads
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Music
drwxr-xr-x 2 user user 120 2011-08-17 18:14 Pictures
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Public
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Templates
-rw-r--r-- 1 user user 3K 2011-08-22 19:47 todo1.txt
-rw-r--r-- 1 user user 1.8K 2011-08-22 18:07 todo2.txt
-rw-r--r-- 1 user user 5K 2011-08-22 18:07 todo3.txt
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Videos
$

Long listing format with file size:


$ ls -ls
total 4
0 drwxr-xr-x 2 user user 80 2011-08-17 16:52 Desktop
0 drwxr-xr-x 2 user user 40 2011-08-17 16:52 Documents
0 drwxr-xr-x 2 user user 40 2011-08-17 16:52 Downloads
0 drwxr-xr-x 2 user user 40 2011-08-17 16:52 Music
0 drwxr-xr-x 2 user user 120 2011-08-17 18:14 Pictures
0 drwxr-xr-x 2 user user 40 2011-08-17 16:52 Public
0 drwxr-xr-x 2 user user 40 2011-08-17 16:52 Templates
4 -rw-r--r-- 1 user user 131 2011-08-17 18:07 todo.txt
0 drwxr-xr-x 2 user user 40 2011-08-17 16:52 Videos
$

Long listing format sorted by file size:


$ ls -lS
total 4
-rw-r--r-- 1 user user 131 2011-08-17 18:07 todo.txt
drwxr-xr-x 2 user user 120 2011-08-17 18:14 Pictures
drwxr-xr-x 2 user user 80 2011-08-17 16:52 Desktop
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Documents
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Downloads
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Music
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Public
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Templates
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Videos
$

Long listing format sorted by date/time:


$ ls -lt
total 4
drwxr-xr-x 2 user user 120 2011-08-17 18:14 Pictures
-rw-r--r-- 1 user user 131 2011-08-17 18:07 todo.txt
drwxr-xr-x 2 user user 80 2011-08-17 16:52 Desktop
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Documents
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Downloads
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Music
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Public
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Templates
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Videos
$

Long listing format in reverse order:


$ ls -lr
total 4
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Videos
-rw-r--r-- 1 user user 131 2011-08-17 18:07 todo.txt
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Templates
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Public
drwxr-xr-x 2 user user 120 2011-08-17 18:14 Pictures
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Music
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Downloads
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Documents
drwxr-xr-x 2 user user 80 2011-08-17 16:52 Desktop
$

Long listing format sorted by date/time in reverse order:


$ ls -ltr
total 4
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Videos
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Templates
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Public
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Music
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Downloads
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Documents
drwxr-xr-x 2 user user 80 2011-08-17 16:52 Desktop
-rw-r--r-- 1 user user 131 2011-08-17 18:07 todo.txt
drwxr-xr-x 2 user user 120 2011-08-17 18:14 Pictures
$

Long listing format with recursive list:


$ ls -lR
.:
total 4
drwxr-xr-x 2 user user 80 2011-08-17 16:52 Desktop
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Documents
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Downloads
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Music
drwxr-xr-x 2 user user 120 2011-08-17 18:14 Pictures
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Public
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Templates
-rw-r--r-- 1 user user 131 2011-08-17 18:07 todo.txt
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Videos

./Desktop:
total 12
-rwxr-xr-x 1 user user 203 2011-08-17 16:52 examples.desktop
-rwxr-xr-x 1 user user 7672 2011-08-17 16:19 ubiquity-
gtkui.desktop

./Documents:
total 0

./Downloads:
total 0

./Music:
total 0

./Pictures:
total 556
-rw-r--r-- 1 user user 94114 2011-08-17 18:13 ls-a.png
-rw-r--r-- 1 user user 254129 2011-08-17 18:14 ls-full-path.png
-rw-r--r-- 1 user user 102761 2011-08-17 18:13 ls-l.png
-rw-r--r-- 1 user user 77374 2011-08-17 18:12 ls-s.png

./Public:
total 0

./Templates:
total 0

./Videos:
total 0
$

ls -r -R command in Linux
ls -r option flag lists files/directories in reverse order.
ls -R option flag lists directory tree recursively.
 ls -r
 ls -R

ls -r
ls -r option flag lists files/directories in reverse order.

Syntax
$ ls -r [options] [file|dir]

Examples
Default list:
$ ls
Desktop Downloads Pictures Templates Videos
Documents Music Public todo.txt
$

List in reverse order:


$ ls -r
Videos Templates Pictures Downloads Desktop
todo.txt Public Music Documents
$

Long listing format in reverse order:


$ ls -lr
total 4
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Videos
-rw-r--r-- 1 user user 131 2011-08-17 18:07 todo.txt
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Templates
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Public
drwxr-xr-x 2 user user 120 2011-08-17 18:14 Pictures
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Music
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Downloads
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Documents
drwxr-xr-x 2 user user 80 2011-08-17 16:52 Desktop
$
Long listing format sorted by date/time in reverse order:
$ ls -ltr
total 4
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Videos
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Templates
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Public
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Music
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Downloads
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Documents
drwxr-xr-x 2 user user 80 2011-08-17 16:52 Desktop
-rw-r--r-- 1 user user 131 2011-08-17 18:07 todo.txt
drwxr-xr-x 2 user user 120 2011-08-17 18:14 Pictures
$

ls -R
ls -R option flag lists directory tree recursively.

Syntax
$ ls -R [options] [file|dir]

Examples
Recursive listing:
$ ls -R
.:
Desktop Downloads Pictures Templates Videos
Documents Music Public todo.txt

./Desktop:
examples.desktop ubiquity-gtkui.desktop

./Documents:

./Downloads:

./Music:

./Pictures:
ls-a.png ls-full-path.png ls-l.png ls-s.png

./Public:

./Templates:

./Videos:
$

Long listing format with recursive listing:


$ ls -lR
.:
total 4
drwxr-xr-x 2 user user 80 2011-08-17 16:52 Desktop
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Documents
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Downloads
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Music
drwxr-xr-x 2 user user 120 2011-08-17 18:14 Pictures
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Public
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Templates
-rw-r--r-- 1 user user 131 2011-08-17 18:07 todo.txt
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Videos

./Desktop:
total 12
-rwxr-xr-x 1 user user 203 2011-08-17 16:52 examples.desktop
-rwxr-xr-x 1 user user 7672 2011-08-17 16:19 ubiquity-
gtkui.desktop

./Documents:
total 0

./Downloads:
total 0

./Music:
total 0

./Pictures:
total 556
-rw-r--r-- 1 user user 94114 2011-08-17 18:13 ls-a.png
-rw-r--r-- 1 user user 254129 2011-08-17 18:14 ls-full-path.png
-rw-r--r-- 1 user user 102761 2011-08-17 18:13 ls-l.png
-rw-r--r-- 1 user user 77374 2011-08-17 18:12 ls-s.png

./Public:
total 0

./Templates:
total 0

./Videos:
total 0
$

s -s/-S command in Linux


ls -s option flag lists file size.
ls -S option flag sorts files/directories list by file size.
 ls -s
 ls -S

ls -s
ls -s option flag lists file size.

Syntax
$ ls -s [options] [file|dir]

Examples
Default listing :
$ ls
Desktop Downloads Pictures Templates Videos
Documents Music Public todo.txt
$

List with file size:


$ ls -s
total 4
0 Desktop 0 Downloads 0 Pictures 0 Templates 0 Videos
0 Documents 0 Music 0 Public 4 todo.txt
$

Long listing format with file size:


$ ls -ls
total 4
0 drwxr-xr-x 2 user user 80 2011-08-17 16:52 Desktop
0 drwxr-xr-x 2 user user 40 2011-08-17 16:52 Documents
0 drwxr-xr-x 2 user user 40 2011-08-17 16:52 Downloads
0 drwxr-xr-x 2 user user 40 2011-08-17 16:52 Music
0 drwxr-xr-x 2 user user 120 2011-08-17 18:14 Pictures
0 drwxr-xr-x 2 user user 40 2011-08-17 16:52 Public
0 drwxr-xr-x 2 user user 40 2011-08-17 16:52 Templates
4 -rw-r--r-- 1 user user 131 2011-08-17 18:07 todo.txt
0 drwxr-xr-x 2 user user 40 2011-08-17 16:52 Videos
$

ls -S
ls -S option flag sorts files/directories list by file size.

Syntax
$ ls -S [options] [file|dir]

Examples
Default list:
$ ls
Desktop Downloads Pictures Templates Videos
Documents Music Public todo.txt
$
List sorted by file size:
$ ls -S
todo.txt Desktop Downloads Public Videos
Pictures Documents Music Templates
$

Long listing format sorted by file size:


$ ls -lS
total 4
-rw-r--r-- 1 user user 131 2011-08-17 18:07 todo.txt
drwxr-xr-x 2 user user 120 2011-08-17 18:14 Pictures
drwxr-xr-x 2 user user 80 2011-08-17 16:52 Desktop
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Documents
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Downloads
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Music
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Public
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Templates
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Videos
$

ls -t command in Linux
ls -t option flag sorts files/directories list by time/date.

Syntax
$ ls -t [options] [file|dir]

Examples
Default list:
$ ls
Desktop Downloads Pictures Templates Videos
Documents Music Public todo.txt
$

List sorted by time/date


$ ls -t
Pictures Desktop Downloads Public Videos
todo.txt Documents Music Templates
$

Long listing format sorted by date/time:


$ ls -lt
total 4
drwxr-xr-x 2 user user 120 2011-08-17 18:14 Pictures
-rw-r--r-- 1 user user 131 2011-08-17 18:07 todo.txt
drwxr-xr-x 2 user user 80 2011-08-17 16:52 Desktop
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Documents
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Downloads
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Music
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Public
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Templates
drwxr-xr-x 2 user user 40 2011-08-17 16:52 Videos
$

ls with full path name in Linux


How to list files and directories to show full path / absolute path name in the Linux
terminal's command shell.

In order to get absolute directory name with ls, enter in the terminal's command shell:
$ ls -d $PWD/*

Example
ls with absolute directory name:
$ ls -d $PWD/*
/home/user/Desktop /home/user/Music /home/user/Templates
/home/user/Documents /home/user/Pictures /home/user/todo.txt
/home/user/Downloads /home/user/Public /home/user/Videos
$

ls with long listing format (-l) absolute directory name (-d):


$ ls -ld $PWD/*
total 4
drwxr-xr-x 2 user user 80 2011-08-17 16:52 /home/user/Desktop
drwxr-xr-x 2 user user 40 2011-08-17 16:52 /home/user/Documents
drwxr-xr-x 2 user user 40 2011-08-17 16:52 /home/user/Downloads
drwxr-xr-x 2 user user 40 2011-08-17 16:52 /home/user/Music
drwxr-xr-x 2 user user 120 2011-08-17 18:14 /home/user/Pictures
drwxr-xr-x 2 user user 40 2011-08-17 16:52 /home/user/Public
drwxr-xr-x 2 user user 40 2011-08-17 16:52 /home/user/Templates
-rw-r--r-- 1 user user 131 2011-08-17 18:07 /home/user/todo.txt
drwxr-xr-x 2 user user 40 2011-08-17 16:52 /home/user/Videos
$

Das könnte Ihnen auch gefallen