working with directories
PWD command:
pwd
: displays your current directory.
CD Command:
cd
: change your current directory with the cd command
cd ~
or cd
: to get back into your home directory
cd ..
: To go to the parent directory
cd -
: to go to the previous directory
Absolute path:
cd dipak/files
Relative path:
cd /home/dipak/files
List Command:
ls
: list the content of directory.
ls -a
: -a used to show all the file. Showing all files means including the hidden files.
ls -l
: -l
gives you long listing data in the current directory
ls -lh
: It shows the numbers (file sizes) in a more human readable format.
Directory Commands:
mkdir
:
mkdir -p
:
rmdir
:
rmdir -p
:
Working with files:
All files are case sensitive.
touch command:
touch
: Used to create an empty file
touch -t
: Used to set some properties while creating empty files
touch -t 200505050000 SinkoDeMayo
output:
-rw-r--r-- 1 paul paul 0 May 5 2005 SinkoDeMayo
Remove Command:
rm
: used to remove the file.he command line in general does not have a waste bin or trash can to recover files. When you use rm to remove a file, the file is gone. Therefore, be careful when removing files!
rm -i
: To prevent yourself from accidentally removing a file, you can type rm -i. It will give you chance to think about to remove file or not.
rm -rf
: Bydefault, rm -r will not remove non-empty directories. The rm -rf statement is famous because
it will erase anything (providing that you have the permissions to do so). be very careful with rm -rf (the f means force and the r means recursive) since being root implies that permissions don’t apply to you. You can literally erase your entire file system by accident.
copy Command:
cp
: To copy a file, use cp with a source and a target argument.
cp -r
: To copy complete directories, use cp -r (the -r option forces recursive copying of all files
copy multiple files
: You can also use cp to copy multiple files into a directory.
cp file42 file42.copy SinkoDeMayo dir42/
cp -i
: To prevent cp from overwriting existing files, use the -i (for interactive) option.
Move Command:
rename files with mv
: Use mv to rename a file or to move the file to another directory.
rename directories with mv
: The same mv command can be used to rename directories.
mv -i
: mv -i will ask permission to overwrite an existing file.
rename Command: It uses regular expressions (regular expression or shor regex are explained in a later chapter) to rename many files at once.
Below a rename example that switches all occurrences of txt to png for all file names ending
in .txt.
rename 's/\.txt/\.png/' *.txt
##############
paul@debian7:~/test42$ ls
abc.txt file33.txt file42.txt
paul@debian7:~/test42$ rename 's/\.txt/\.png/' *.txt
paul@debian7:~/test42$ ls
abc.png file33.png file42.png
This second example switches all (first) occurrences of file into document for all file names
ending in .png.
paul@debian7:~/test42$ ls
abc.png file33.png file42.png
paul@debian7:~/test42$ rename 's/file/document/' *.png
paul@debian7:~/test42$ ls
abc.png document33.png document42.png
paul@debian7:~/test42$
Working with file contents:
head Command:
head
: head comamnd is used to display the first ten lines of a file
.
head /etc/passwd
:
head -4 /etc/passwd
: It display the first n lines of a file.
head -c14 /etc/passwd
: It display the first n bytes.
tail command: tail command will used display the last ten lines of a file
.
tail /etc/services
:
tail -3 /etc/services
:
cat Comamnd: It copies standard input to standard output. cat is also used to display a file on the screen.
Display the files
: Used to display a file on the screen.
cat /etc/resolv.conf
concatenate files into a bigger (or complete) file
: cat is short for concatenate One of the basic uses of cat is to concatenate files into a bigger (or complete) file.
paul@debian8:~$ echo one >part1
paul@debian8:~$ echo two >part2
paul@debian8:~$ echo three >part3
paul@debian8:~$ cat part1
one
paul@debian8:~$ cat part2
two
paul@debian8:~$ cat part3
three
paul@debian8:~$ cat part1 part2 part3
one
two
three
paul@debian8:~$ cat part1 part2 part3 >all
paul@debian8:~$ cat all
one
two
three
Create the files
: Used to create flat text files. Then type one or more lines, finishing each line with the enter key. After the last line, type and hold the Control (Ctrl) key and press d.
paul@debian8:~$ cat > winter.txt
It is very cold today!
paul@debian8:~$ cat winter.txt
It is very cold today!
custom end marker
: You can choose an end marker for cat with << as is shown in this screenshot. This construction is called a here directive and will end the cat command.
paul@debian8:~$ cat > hot.txt <<stop
> It is hot today!
> Yes it is summer.
> stop
paul@debian8:~$ cat hot.txt
It is hot today!
Yes it is summer.
copy files
: cat can be used to copy files.
paul@debian8:~$ cat winter.txt
It is very cold today!
paul@debian8:~$ cat winter.txt > cold.txt
paul@debian8:~$ cat cold.txt
It is very cold today!
tac Command: It display the files output from last line to first line.
paul@debian8:~$ cat count
one
two
three
four
paul@debian8:~$ tac count
four
three
two
one
More and Less Command: The more command is useful for displaying files that take up more than one screen. More will allow you to see the contents of the file page by page. Use the space bar to see the next page, or q to quit.