Building Bash Muscle (4)

I want to focus on file/directory related stuff today. I use these commands pretty much daily but I would like to get my brain organized and possibly learn some new techniques along the way. I’m just going to randomly come up with possibly useful commands.

Where am I?

$ pwd
output: /home/amaterasu48

That was too basic.

List only files in the current directory

$ ls -al | grep '^-'

If I explain what it means, ls is to list directory contents. -l means “use a long listing format”. -a means all entries including the ones that start with . The grep part filters the result. '^-' means the entries that starts with dash. The output looks like the following.

-rw-------  1 amaterasu48 amaterasu48  31956 Sep 30 21:11 .bash_history
-rw-r--r--  1 amaterasu48 amaterasu48    220 Apr  7 15:17 .bash_logout
-rw-r--r--  1 amaterasu48 amaterasu48   4284 May 19 19:56 .bashrc
-rw-r--r--  1 amaterasu48 amaterasu48     23 Sep 18 23:25 .dmrc
-rw-rw-r--  1 amaterasu48 amaterasu48     56 May 19 21:53 .gitconfig
-rw-r--r--  1 amaterasu48 amaterasu48     22 Apr  7 15:17 .gtkrc-2.0
-rw-r--r--  1 amaterasu48 amaterasu48    516 Apr  7 15:17 .gtkrc-xfce
-rw-------  1 amaterasu48 amaterasu48  32422 Sep 29 12:50 .ICEauthority
-rw-------  1 amaterasu48 amaterasu48    144 Oct  1 20:53 .lesshst
-rw-r--r--  1 amaterasu48 amaterasu48    258 Apr  7 17:16 .pam_environment
-rw-r--r--  1 amaterasu48 amaterasu48    807 Apr  7 15:17 .profile
-rw-------  1 amaterasu48 amaterasu48      7 Aug  2 10:53 .python_history
-rw-r--r--  1 amaterasu48 amaterasu48      0 Apr  7 15:30 .sudo_as_admin_successful
-rw-------  1 root        root          8504 Sep 30 21:22 .viminfo
-rw-rw-r--  1 amaterasu48 amaterasu48     14 Sep  7 22:09 .vimrc
-rw-rw-r--  1 amaterasu48 amaterasu48    344 Aug  4 19:01 .wget-hsts
-rw-------  1 amaterasu48 amaterasu48     55 Sep 29 12:50 .Xauthority
-rw-r--r--  1 amaterasu48 amaterasu48    132 Apr  7 17:18 .xinputrc
-rw-------  1 amaterasu48 amaterasu48 186154 Oct  1 20:52 .xsession-errors
-rw-------  1 amaterasu48 amaterasu48 432810 Sep 29 12:24 .xsession-errors.old

How many files are there in the current directory?

We can reuse the previous command the pipe it with wc -l. wc means “print newline, word, and byte counts for each file”. -l is an option that “prints the newline counts.” The command looks like this.

$ ls -al | grep '^-' | wc -l

How many directories are there in the current directory?

Again, applying the previous technique, this is quite easy.

$ ls -la | grep '^d' | wc -l

What is the size of the current directory?

$ du -sh

What if I want to check the size of a specific directory? Just add the full path at the end of the command.

$ du -sh /home

du is estimate file space usage. -s option “displays only a total for each argument.”

Sort by date

There are times when I think “What was modified most recently?” Here is the command for it.

$ ls -lat

The output looks like the following.

total 52
drwxrwxr-x 10 amaterasu48 amaterasu48 4096 Oct  1 23:27 .
-rw-r--r--  1 amaterasu48 amaterasu48 9709 Oct  1 23:27 ort
drwxr-xr-x 72 amaterasu48 amaterasu48 4096 Oct  1 23:26 ..
drwxrwxr-x  7 amaterasu48 amaterasu48 4096 Sep 29 22:42 control-repo
drwxrwxr-x  5 amaterasu48 amaterasu48 4096 Sep 24 21:03 playground
drwxrwxr-x  9 amaterasu48 amaterasu48 4096 Sep 22 09:35 rtl8814au
drwxrwxr-x  3 amaterasu48 amaterasu48 4096 Sep 18 22:28 cmdb-manager
drwxrwxr-x  6 amaterasu48 amaterasu48 4096 Sep 18 11:24 jenkins-python
drwxrwxr-x  5 amaterasu48 amaterasu48 4096 Sep 14 22:02 docker
drwxrwxr-x  5 amaterasu48 amaterasu48 4096 Jul 29 22:07 jenkins-lib
drwxrwxr-x  3 amaterasu48 amaterasu48 4096 Jul 17 08:53 bash-utilities

-t option means “sort by modification time, newest first.”

Find files that contains certain text

$ grep -rli 'test'

If you want to limit the file type, you can add –include like the following example.

sudo grep -rli 'test' --include=\*.txt

OK, that’s it for today. Keep on digging!

Author: admin

A software engineer in greater Seattle area

Leave a Reply

Your email address will not be published. Required fields are marked *