pwd
ls -la
, -ltrh
cd
pushd
(push a dir on top of stack and goto it) popd
(go back to dir on top of stack)
# in dir A
$ pushd B/C
$ popd
# in dir A again
# Shortcut: cd -
touch
(update time modified/create new)
file
(show if an object is dir or what kind of file it is)
$ file a b
a: directory
b: ASCII text
du
(show disk usage of a file/dir)
df
(show free disk space info of partitions)
$ du foo
12 foo
$ du - h foo # human readable sizes
12K foo
mkdir -p
(p flag is used to make nested dirs like mkdir /foobar/test/mydir
)
rmdir
(removes only empty dirs; shouldn’t have any subdirectories too)
rm
(remove)
$ rm fileA fileB ...
-r to remove dir
-f force remove even if write protected
-i prompt before removal of each dir
-d remove if empty dir only
cp
(copy) (overwrites by default)
$ cp fileA fileB /path_to_target_dir/...
-r to copy dir
-i prompt before overwrite
dd
(copies contents of one file into another) (creates new if doesn’t exist)
$ dd if=old_file of=new_file
mv
(move, rename) (overwrites by default) (no r flag needed for dir)
$ mv fileA fileB /path_to_target_dir/...
$ mv old_name new_name
-i prompt before overwrite
tree
(print dir tree)
find
(recursively search a file or dir)
# current dir is the deafult
$ find -name foo.txt
# by name, by type
$ find -name foo.txt -type f
# specify a dir
$ find /my/dir/ -name puppies.jpg
# empty files and dir
$ find /my/dir -empty
# multiple dir, wildcards
$ find dir1 dir2 -name *ar.png
# need quotoes in these kind of wildcards
$ find dir1 dir2 -name "*.png"
# search by path
$ find . -path "*app-logs*"
# conditionals - OR (default is AND) (-and -not are also available)
$ find . -name foo -or -type f
# case-insensitive searches (-iXXXX)
$ find -iname "fox" -ipath "*myapp*"
# by permission
$ find . -perm 700
# files greater than 3Mb
$ find . -size +3M
# files smaller than 200 bytes
$ find . -size -200c
#files greater than 3Mb but less than 500Mb
$ find . -size +3M -500M
# accessed within last 3 days
$ find . -atime -3
# modified within last 2 days
$ find . -mtime -2
# actions on search results:
# print full path
$ find / -name "myfile" -print
# delete (use with extreme caution!)
$ find / -name "myfile" -delete
The find
command doesn’t follow symlinks by deafult, so when we search in /bin
it shows as empty, but ls
shows a lot of files. Use -L
flag with find
command to follow symlinks.
ln
(create links to file or dir)
# hardlink
$ ln file shortcut_file
# softlink
$ ln -s file shortcut_file
# unlinking deletes the shortcut_file for both hard and soft links
$ unlink shortcut_file
Theory: /linux/fs/#inodes-and-links
Filenames and directory names are case-sensitive in Linux.
A file and a directory having exact same names cannot co-exist in the same directory.
cp
and mv
commands can’t copy or move directories inside themselves.
$ cp -r a a/b
cp: cannot copy a directory, 'a', into itself, 'a/b/a'
$ mv a a/b
mv: cannot move 'a'c to a subdirectory of itself, 'a/b/a'
Use cat > fileC.txt
to create a file if fileC
is already present in the current dir since touch
will just update it’s access time.
ls | grep <string>
can be used instead of the find
command if the search is by file name only.
Using wildcards in commands to filter files and dir is called File globbing. Ex - ls *.txt
* match multiple chars
? match single char
[abc] match A or B or C
[^ab] match neither A nor B
[!ab] match neither A nor B
[a-k] match all chars from A to K
[a-z0-9] match A to Z and 0 to 9
$ ls *.mp3 lists all mp3 files
$ ls ?est lists "test", "best"
$ ls [tb]est lists test, best
$ ls [^t]est lists "best" only