Escolar Documentos
Profissional Documentos
Cultura Documentos
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
$ cat list2.txt
house
car
house
car
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
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 -n no file overwrite
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/
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
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
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/
pwd syntax
$ pwd [option]
Change directory to parent directory of the home directory and print working directory:
$ cd ~/..
$ pwd
/home
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]
Change directory to parent directory of the home directory and print working directory:
$ cd ~/..
$ pwd
/home
$ cat list2.txt
house
car
$ cat list2.txt
house
car
$
Combine 2 text files:
$ cat list1.txt list2.txt
milk
bread
apples
house
car
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 -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 -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
$
./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
$
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:
$
./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 -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
$
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
$
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
$
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
$