User Tools

Site Tools


cheatsheet:linux_basic_file_manipulation

linux basic file manipulation command cheatsheet

filesystem navigation

# see current folder:
$ pwd
 
# change folder forward:
$ cd Documents/ # one level
$ cd Documents/financereports/ # 2 levels
 
# change folder:
$ cd .. #one level
$ cd ../../.. # jump 3 levels back
$ cd ../Documents # jump 1 level back, and 1 level up again

listing files

$ ls # simple list files
authorized_keys  config  id_rsa  id_rsa.pub  known_hosts  known_hosts.old something  sftp  sftp.pub  sftp_base64
 
 
$ ls -a # list all files (also hidden and system files)
.  ..  authorized_keys  config  id_rsa  id_rsa.pub  known_hosts  known_hosts.old  something  sftp sftp.pub  sftp_base64
 
$ ls -al # list all files, and display as detailed list ((modification)date&time, size, permissions, ownership)
total 52
drwx------  2 ubuntu ubuntu 4096 Nov 25 23:20 .
drwxr-x--- 11 ubuntu ubuntu 4096 Dec  8 14:50 ..
-rw-------  1 ubuntu ubuntu  398 Oct 31 21:58 authorized_keys
-rw-rw-r--  1 ubuntu ubuntu  479 Nov 25 23:20 config
-rw-------  1 ubuntu ubuntu 2610 Oct 31 21:58 id_rsa
-rw-r--r--  1 ubuntu ubuntu  580 Oct 31 21:58 id_rsa.pub
-rw-------  1 ubuntu ubuntu 3994 Nov 25 23:20 known_hosts
-rw-------  1 ubuntu ubuntu 3630 Nov 25 21:35 known_hosts.old
-rw-------  1 ubuntu ubuntu 1679 Oct 21 10:54 something
-rw-------  1 ubuntu ubuntu 3389 Oct 31 18:22 sftp
-rw-------  1 ubuntu ubuntu  746 Oct 31 18:22 sftp.pub
-rw-------  1 ubuntu ubuntu 4557 Nov 23 15:46 sftp_base64
 
$ ls -alt # list all files, display as list, sorted by (modification) time
total 52
drwxr-x--- 11 ubuntu ubuntu 4096 Dec  8 14:50 ..
-rw-------  1 ubuntu ubuntu 3994 Nov 25 23:20 known_hosts
drwx------  2 ubuntu ubuntu 4096 Nov 25 23:20 .
-rw-rw-r--  1 ubuntu ubuntu  479 Nov 25 23:20 config
-rw-------  1 ubuntu ubuntu 3630 Nov 25 21:35 known_hosts.old
-rw-------  1 ubuntu ubuntu 4557 Nov 23 15:46 sftp_base64
-rw-r--r--  1 ubuntu ubuntu  580 Oct 31 21:58 id_rsa.pub
-rw-------  1 ubuntu ubuntu 2610 Oct 31 21:58 id_rsa
-rw-------  1 ubuntu ubuntu  398 Oct 31 21:58 authorized_keys
-rw-------  1 ubuntu ubuntu 3389 Oct 31 18:22 sftp
-rw-------  1 ubuntu ubuntu  746 Oct 31 18:22 sftp.pub
-rw-------  1 ubuntu ubuntu 1679 Oct 21 10:54 something

display content of files

# create new empty file
$ touch filename.ext
 
# echo data into file
$ echo "some string with some data" > filename.ext
 
# display content for file
$ cat filename.ext
some string with some data
 
# display large files, allowing scrolling (horizontal) & display multiple files at once separated by lines
$ more filename.ext
 
# display large files, allowing scrolling (horizontal & vertical) & display multiple files with switching mode + adding basic "vi" command support
$ less filename.ext
 
# display last 10 lines of a file
$ tail filename.ext
$ tail -n99 filename.ext # show last 99 lines
 
# display last 10 lines of a file, and keep following it (echoing all new lines that are written to the document). Very useful for monitoring log files
$ tail -f filename.ext
 
# display first 10 lines of a file
$ head filename.ext
$ head -n2 filename.ext # show first 2 lines only

write to file

tee (t-splitter)

“Reads from stdout, writes to a) stdout or b) one or more files”

$ date | tee mytime.txt
Sat Nov 19 00:03:32 UTC 2022
 
$ cat mytime.txt
Sat Nov 19 00:04:03 UTC 2022
 
#(doing this with "date > mytime.txt" instead, would not display the output on the screen/stdout, but only into a file)

write to multiple files & stdout

$ date | tee file1.txt file2.txt file3.txt
Sat Nov 19 00:06:30 UTC 2022
 
$ ls -al
total 32
drwx------  2 root root 4096 Nov 19 00:06 .
drwxr-xr-x 91 root root 4096 Nov 18 23:47 ..
-rw-------  1 root root   29 Nov 19 00:06 file1.txt
-rw-------  1 root root   29 Nov 19 00:06 file2.txt
-rw-------  1 root root   29 Nov 19 00:06 file3.txt

add (append) a line to a file that is owned by root

(this is super useful when to add config to configuration files on your system)

$ echo "newline" | sudo tee -a /etc/file.conf
newline
cheatsheet/linux_basic_file_manipulation.txt · Last modified: 2023/02/13 23:16 by kamaradski