my public linux cheat sheet containing some commands I might use & a short explanation of each one
| Topic |
|---|
| Basic commands |
| File and strings |
| Command |
|---|
| pwd |
| ls |
| history |
| mkdir -p |
| rmdir |
| cat |
| cp |
| mv |
| rm |
- pwd shows your current directory
$ pwd
/home/mobin- ls lists files and directories
$ ls
HW slidesthere are different options for ls command:
-t : sort by last modified (newest first)
-l : more detailed
$ ls -l
total 4
drwxrwxrwx 1 mobin mobin 0 Oct 11 16:41 HW
drwxrwxrwx 1 mobin mobin 4096 Sep 30 10:14 slides
-a : show everything including hidden
-R : goes into every directory and recursively lists
-h : shows memory section human readable (should be used with -l)- history shows previously executed commands
$ history
...
2203 ls
2204 ls -l
2205 history
2206 pwd- mkdir -p creates parent directories recursively
mkdir -p [new directory name]/[the insider directory name inside the new directory] $ mkdir -p d/inside-d
$ ls -R
.:
d
./d:
inside-d
./d/inside-d:- rmdir removes empty directories
$ rmdir [directory1 name] [directory2 name] ...we can force it to remove non-empty directories but it's more common to use rm -rf instead
rmdir -p: removes parent directories recursively
$ rmdir -p d/inside-d/inside-inside-d- cat shows file's contents
cat [file name] $ cat a.txt
nothing special here
-n : if you add this option, it shows files line numbers toowe can use this command to add to a file(using the option below everything we type will be saved in a.txt):
$ cat > a.txt- cp copies file or directory to a destination path
copy file:
cp [file you want to copy] [the path you want your file to be copied to and the name of it]make a copy of a.txt in mydir named b.txt
$ cp a.txt mydir/b.txtcopy directory:
we use -r option to copy directories along with their contents
cp -r [directory you want to copy]/ [the path you want your directory to be copied to and the name of it]make a copy of mydir1 to pdir named mydir2
$ cp -r mydir1/ pdir/mydir2- mv cuts file or directory to a destination path, rename a file or directory
move file:
move a.txt to mydir directory
$ mv a.txt mydir/move directory:
move mydir1 directory to mydir2 directory
$ mv mydir1/ mydir2/rename:
rename a.txt to b.txt
$ mv a.txt b.txt- rm removes file or directory
remove a.txt
$ rm a.txtremove a directory
$ rm -r mydir/- ~ denotes a user's home directory
$ cd ~
$ pwd
/home/mobin-
use --help or man to get description of the command you need
-
there are 3 types of standard streams:
- standard input stream: source of input for the program
- standard output stream: used for output from the program
- standard error stream: used for error messages
- pipelines are a kind of redirection in which we use to connect commands and get output from one and give it as input to another
$ cat <<EOF | sort
hello
from
alireza
in quera
to
you
EOF
alireza
from
hello
in quera
to
you| Command |
|---|
| du |
| file |
| find |
| head |
- du shows the space usage of files and directories of the current directory
$ du
680 ./HWs/HW1
680 ./HWs
32752 ./slides/microcontroller_AVR
16536 ./slides/microprocessor
49288 ./slides
230656 ./voices
280624 .as you can see, it recursively shows each directories space usage
we can pass the address to du too
there are some usefull options too:
options:
-h : shows space usage human readable
-sh : just shows current direcotiry- file shows files type
file [file name] $ file README.md
README.md: ASCII text- find finds files and directories
$ find [starting directory to search] -name "[file to search]" find . -name "*.txt"- head
- there are several editors in linux like vim & nano
$ nano a.txt
$ vim a.txt