Approvals: 0/1
File based utilities
Looking around
Looking through files is done with thels
command under Linux, which is an abbreviation forlist
.
By default (meaning without parameters) the ls
command shows the content in the current directory. It does hide elements starting with a dot though. These hidden elements can be shown by adding the -a
parameter, which stands for all
. If you want detailed information about the elements, add the -l
parameter for long listing
. You can also specify a file/directory which you want to examine, by just appending the path to the command.
$ ls # shows files and directories testdir test $ ls -a # includes hidden ones . .. testdir test $ ls -l # detailed view drwxr-xr-x 1 myuser p12345 0 Apr 13 2017 testdir -rw-r--r-- 1 myuser p12345 4 Apr 13 11:55 test $ ls /tmp/ allinea-USERNAME ssh-6E553lWZCM ssh-C6a754pJ1d systemd-private-a4214393983d448fbdc689791806519c-ntpd.service-LrAgBP tmp7CJZRA yum_save_tx.2017-04-03.12-07.VCUowf.yumtx $ ls -alh ~ drwxr-xr-x 5 myuser mygroup 45 Jan 30 2017 .allinea -rw------- 1 myuser mygroup 21K May 3 17:14 .bash_history -rw-r----- 1 myuser mygroup 231 Dec 2 2016 .bashrc drwx------ 1 myuser mygroup 76 Aug 22 2017 Simulation drwx------ 2 myuser mygroup 76 Dec 12 2016 .ssh
Moving around
Moving through directories is done with thecd
command, which stands forchange directory
.
The cd
command can be called without any arguments, in which case it just switches to the home directory. Otherwise it takes a absolute (starting with a dash) or relative path as an argument and switches to that directory. The argument -
(just a single dash) will cause cd to switch to the previous directory. This can be used to alternate between two directories without typing their path’s every time.
$ cd /bin # go to an absolute directory $ cd [~] # go home $ cd - # go to previous directory
Copying & moving files around
Copying and moving files and directories is done with thecp
andmv
commands, which stand forcopy
andmove
respectively.
Both commands take at least two parameters, which correspond to the <SOURCE>
and <DESTINATION>
files or directories. For cp
to work with directories as a source, it needs the -r
(recursive) or -a
(archive) flag.
Beware of the pitfalls! you can overwrite data and therefor lose it, without getting any confirmation prompt! If in doubt use-i
(interactive) flag.
$ mv old new # rename old to new $ mv old dir/ # move old into dir $ mv file1 file2 # overwrite file2 with file1 # (BEWARE)
$ cp -i input input.bak # input to input.bak $ cp -i input backup/ # input into backup $ cp -a dir1/ dir2 # exact copy of dir1
Creating and deleting
directories
creating directories is done with themkdir
command, which stands formake directory
.
The mkdir
command takes an optional -p
(parents) parameter and a path. When optional parameter is given, it will create all the ancestors of the specified directory aswell. Otherwise the command fails if the directory either exists already or its parent doesn’t exist either.
deleting directories is done with thermdir
command, which stands forremove directory
.
This command removes the specified directory, but only if it’s already empty. it can also take an optional -p
parameter, in which case it removes the specified directory and all the ancestors you included.
Finding stuff
To look at everything
- in your home directory
- and nested up to three levels deep inside it
- that ends in
.txt
- or starts with
log_
- and is an ordinary file
- concatenated as one stream:
find \ ~ \ -maxdepth 3 \ -iname "*.txt" \ -or -iname "log_*" \ -type f \ -exec cat '{}' \; | less
find \ ~ \ -maxdepth 3 \ -iname "*.txt" \ -or -iname "log_*" \ -type f \ -exec cat '{}' + | less
Content based utilities
Contents of files
viewing is done by theless
command:
concatenating is done by thecat
command:
$ less file.txt # exit with 'q' $ less -R file.txt # keep colors $ cat file1 file2 | less
$ cat file $ cat -A printable $ cat -n numbered
$ echo "VSC is great" > file $ cat file VSC is great
$ echo "VSC is awesome" >> file $ cat file VSC is great VSC is awesome
$ cat file | grep awesome VSC is awesome
$ grep awesome file VSC is awesome
Space accounting
viewing used space is done by thedu
(disk usage) command:
viewing free space is done by thedf
(disk free) command:
$ du -h file1 file2 # human readable output $ du -s dir # summarize
$ df -h # human readable output $ df -t nfs # only list filesystems of a type
Recap
$ mv space.log space.log.bak
$ df -h | grep "lv12345\|lv54321" > space.log
$ cat space.log
nfs05.ib.cluster:/e/lv12345 200G 185G 16G 93% /home/lv12345 nfs04.ib.cluster:/e/lv54321 1000G 979G 22G 98% /home/lv54321
we do this often, let’s wrap it up!
Recap++
$ mv space.log space.log.bak
$ df -h | grep "lv12345\|lv54321" > space.log
$ cat space.log
nfs05.ib.cluster:/e/lv12345 200G 185G 16G 93% /home/lv12345 nfs04.ib.cluster:/e/lv54321 1000G 979G 22G 98% /home/lv54321
we do this often, let’s wrap it up!
$ echo '#!/bin/bash' > spacelog.sh $ echo 'mv space.log space.log.bak' >> spacelog.sh $ echo 'df -h | grep "lv12345\|lv54321" > space.log' >> spacelog.sh $ echo 'cat space.log' >> spacelog.sh
$ chmod +x spacelog.sh
$ ./spacelog.sh
Sed and awk
sed (stream editor) and awk are powerful tools when working with the command line
$ mycommand | sed "..."
$ mycommand | awk '{...}'
Using sed and awk in action
program | command | description |
---|---|---|
sed | s/old/new/ | replace old with new |
sed | /from/,/to/ s/old/new/ | replace old with new , between from and to |
awk | 'print $5 $3' | print columns 5 and 3 of every line |
Example script:
#!/bin/bash mv space.log space.log.bak df -h | grep "lv12345\|lv54321" > space.log cat space.log
#!/bin/bash mv space.log space.log.bak df -h | grep "lv12345\|lv54321" > space.log cat space.log | sed "s|/home/lv12345|ProjectA|" \ | awk '{print $6, "free:", $4}' \ | column -t
<HTML> <!– ### Sort and uniq
sort and uniq (unique) are used to sort and uniquify adjacent lines
–> </HTML>