Saturday 16 June 2018

commands in Linux


ls command in Linux/Unix
ls is a Linux shell command that lists directory contents of files and directories.
ls syntax
$ ls [options] [file|dir]
ls command options
ls command main options:
option
description
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
list with long format - show permissions
list long format including hidden files
list long format with readable file size
list with long format with file size
list in reverse order
list recursively directory tree
list file size
sort by file size
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 [optionsfile1 [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

cp command syntax

Copy from source to dest
$ cp [optionssource 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
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:
$ cpmain.cbak

Copy 2 files main.c and def.h to destination absolute path directory /home/usr/rapid/ :
$ cpmain.cdef.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/ :
$ cpsrc /home/usr/rapid/

Copy all files and directories in dev recursively to subdirectory bak:
$ cp -R devbak

Force file copy:
$ cp -f test.cbak

Interactive prompt before file overwrite:
$ cp -itest.cbak
cp: overwrite 'bak/test.c'? y

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

mv command in Linux/Unix

Linux mv command.
mv command is used to move files and directories.

mv command syntax

$ mv [optionssource 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.cdef.h files to /home/usr/rapid/ directory:
$ mvmain.cdef.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 :
$ mvbak/* .

Rename file main.c to main.bak:
$ mvmain.cmain.bak

Rename directory bak to bak2:
$ mvbak bak2

Update - move when main.c is newer:
$ mv -u main.cbak
$

Move main.c and prompt before overwrite bak/main.c:
$ mv -v main.cbak
'bak/main.c' -> 'bak/main.c'
$


No comments:

Post a Comment