Linux – commands Cheat Sheet
ls
The command ls displays the contents of a directory.
ls -l
View information in detail.
ls -a
Show hidden files.
ls -h
View the size of files readable way.
ls -r
reverse sorting.
ls -t
Sort files by date from newest to oldest.
ls -S
Sort by decreasing size.
ls -la
Show all files including hidden files.
ls -lhS
Show file information, with readable sizes orderly everything from largest to smallest.
cd
Navigating from one directory to another is done with the command cd taken over the directory name.
cd /
Lets meet at the root of the disc.
cd ~
or cd
Go directly to the user’s directory.
cd /var/www/
Go to the / var / www.
cd ..
Up in the parent directory from where you are.
cd -
Returns to the previous directory.
pwd
Return the absolute path of the current directory which is useful since in general the shell only displays the name of the current directory.
cp
To create a copy of a file, the command is used cp .
cp foo/bar.txt baz/
Bar.txt copy the file in the directory baz.
cp -r foo/ baz/
Copy entire directories (note: if there baz, the target will baz / foo /).
mv
Move a file is as simple as copy, for that you need to use the command mv . This command also allows you to rename your files.
mv foo/bar.txt baz/
Bar.txt move the file in the directory baz.
mv foo_bar.txt foo_baz.txt
Rename the file in foo_bar.txt foo_baz.txt.
rm
To delete a file or directory using the command rm .
rm *.txt
Delete all files with txt extension to.
rm foo.txt bar.txt
Delete files foo.txt and bar.txt.
rm -rf baz/
Remove baz directory and all its contents.
mkdir
To create a directory just use the command mkdir . This will allow you to create a directory to where you are or the location specified in the command argument.
mkdir -v
Return information when creating a directory.
mkdir -p
This option creates a complete tree.
mkdir foo
Create the directory foo.
mkdir -v foo /tmp/bar
Create directories foo and / tmp / bar.
mkdir -p foo/bar/baz
Create the tree foo / bar / baz.
chown
The command chown can change the owner of a file or directory.
chown bob:admin foo.txt
Assign user bob and the admin group foo.txt file.
chmod
The command chmod can change the rights of a file or directory. To execute this command you must be the owner of the file or be logged in as root.
chmod u+w fichier
Add write permissions to the owner (user, write)
chmod g+r fichier
Add read permissions to the file group (group, read)
chmod o-x fichier
Delete the enforcement rights to other users (other, execution)
chmod a+rw dossier
Add read / write at all (all)
chmod -R a+rx files
Add read and execute permissions to anything in the folder directory.
chmod 764 dossier
All rights for the owner (7xx), reading and writing for the group (x6x) and read-only for others (xx4).
chmod -R 755 dossier
Give the owner all rights (7xx), while only read and access will be given to others (x55). Thanks to the -R option rights will be applied to all files and folders in that directory.
rights of representation Matches | ||
---|---|---|
Law | alphanumeric value | octal |
no right | — | 0 |
execution only | –x | 1 |
write only | -w- | 2 |
writing and execution | -wx | 3 |
read only | r– | 4 |
Read and Execute | rx | 5 |
reading and writing | rw- | 6 |
all rights (read, write, and execute) | rwx | 7 |
ssh
The command ssh allows you to securely connect to a remote machine.
ssh john@remotehost.example.com
Connection to the remote machine with the login john.
ssh -l john remotehost.example.com
Equivalent to the previous command.
ssh-keygen -t dsa
Generating a DSA key (to be on the local machine).
ssh-copy-id -i ~/.ssh/id_dsa.pub john@remotehost.example.com
Copy the public key to the remote machine.
scp
The command scp to copy files between the server and the client ssh securely.
scp foo.txt john@remotehost.example.com:
Transfers the file foo.txt located in the current directory to the home of the account john remotehost.example.com machine.
scp john@remotehost.example.com:foo.txt ./
Retrieves the file foo.txt located in the home of john account directory to copy it to the current directory.
scp john@remotehost.example.com:/backups/*.sql backups/
Recover .sql files in the directory to copy the backups to the backups subdirectory.
scp -P 17654 john@remotehost:/files/ files/
Recover files via another port (17654) as the default port (22).
scp -r mails/ john@remotehost:
Transfers the entire email directory.
rsync
With the command rsync can copy files and folders to or from a remote host. With this you can sync files.
- Some options:
- -a : copy recursively while preserving file permissions and dates.
- -z : compress the data before transfer.
- -v : displays everything that happens during the transfer.
- -n : to test the ( dry-run ).
- –progress : displays the progress bar during the transfer.
- –delete : delete files that do not exist in the transmitter
- –exclude = PATTERN : excludes files that match the pattern
rsync source/ destination/
Synchronize the files source to a destination.
rsync -azv john@remotehost.example.com:source/ destination/
Retrieves new files and changed files from remote directory to the local directory.
rsync -azv --delete source/ destination/
Synchronize all files, also delete files that no longer exist on the source.
rsync -azv --exclude="dump/*.sql.gz" files/ backup/
Synchronize all the files directory except .sql.gz files.
rsync --rsh='ssh -p1234' ource/ destination/
Perform rsync on a specific port.
Note: In general, we always by a suffix /
the name of the source directory and destination.
sed
The command sed is a utility that runs a text file line by line to apply a treatment or replacement when the regular expression is checked.
sed 's/foo/bar/' file.txt
Transforming the first occurrence ‘foo’ to each line with ‘bar’.
sed 's/\t/ /g' file.txt
Transform all tabs with two spaces.
sed '/^#/ d' file.txt
Remove all lines beginning with #.
sed '/^Bonjour/,/^Au revoir/d' file.txt
Delete all lines between the two patterns.
sed -n '/foo/p' file.txt
Show only lines where the expression is found.
grep
The command grep allows you to search a string or pattern in a file.
- Some options:
- -v : displays the lines do not contain the string.
- -c : count the number of lines containing the string.
- -n : return lines prefixed by number.
- -x : lines matching the string.
- -l : displays the names of files that contain the string.
grep 'text' foo.txt
Search this case ‘text’ in the foo.txt file.
grep -nri 'foobar' /project
Search all occurrences of ‘foobar’ in the directory / project.
grep -nri '\(foo\|bar\|baz\)' /project
Search all occurrences to ‘foo’, ‘bar’ and ‘baz’ in the directory / project.
find
The command find allows searching files and possibly execute commands above.
- Some options:
- -name : Search for a file by name
- -iname : Same as -name but insensitive
- -type : File search of a certain type
- -atime : Search by last access
- -mtime : Search by last modification date
- -user : Finding files belonging to the given user
- -group : Finding files belonging to the given group
- -size : Research versus file size.
- exec : Runs the command given to the files found.
- -a : AND Operator
- -o : OR Operator
- ! or -not : NOT Operator
find myfile* -print
Find a file beginning with “myfile”
find -name *myfile*.txt -print
Search for a file “myfile” and having the extension “.txt”
find /usr -type d -print
View all of / usr directory
find $HOME \( -name '*.txt' -o -name '*.pdf' \)
View all .txt or .pdf files in the home directory of the user.
find $HOME -name *.txt -atime +7 -exec rm {} \;
Delete all .txt files that have not been accessed for more than seven days in the home directory of the user.
find $HOME -name '*.txt' -size +4k -exec ls -l {} \;
View the size of all files of more than 4 kilos
test
The control test allows comparison of strings, numbers and verify some property files. It is possible to simplify the writing by replacing the brackets [].
test -e foo.txt
foo.txt test if the file exists.
test -d foo
test that foo is a directory.
test -w foo.txt
test if file is writable.
test -x foo.txt
test if file is executable.
[ "string1" != "string2" ]
check that the chain is not string1 equal to string2.
[ int1 != int2 ]
check that the number is less than int1 int2.
packages
Installing and uninstalling packages in Debian, Ubuntu and derivative distribution can be done with the command apt .
apt-get update
Update the list of files available in the repositories APT.
apt-get install samba
Install the Samba package.
apt-get install foo=2.2-1
Install the foo package in its 2.2-1 release.
apt-get remove samba
Samba package Uninstalling while leaving the configuration files.
apt-get purge samba
complete removal of the Samba package and its configuration files.
apt-cache policy php5
Retrieving information about the status of package php5
dpkg -l | grep php
Php list all packages installed on the machine
archives
Whether to compress and decompress files or folders, this is done with the command tar .
- Some options:
- -c : create
- -t : test / list
- -x : extract
- -v : description of the unpacked files
- j : bzip2 compression format
- -z : gzip compression format
tar -cvf archive.tar fichier1
Creating an archive named archive.tar contains the file1 file.
tar -cvf archive.tar fichier1 fichier2
Create an archive containing both file1 and file2.
tar -cvf archive.tar repertoire/
Create an archive from a directory.
tar -czvf archive.tar.gz repertoire/
Create an archive in tar.gz format
tar -cjvf archive.tar.bz2 repertoire/
Create an archive in tar.bz2 format.
tar -xzvf archive.tar.gz
Extraction of tar.gz archive
tar -xjvf archive.tar.bz2
Extracting the tar.bz2 archive.
tar -tf mon_fichier.tar
List all files in an archive.
disk space
du -sh dossier1 dossier2
know the disk space of two directories ( disk usage ).
du -hc --max-depth=1
display disk space usage of files and directories contained in a directory.
df -h
display disk space ( disk free ).
Leave a Reply