DEV Community

Le Vuong
Le Vuong

Posted on

Bash tutorial for popular commands: ls / find / grep

# List files in current folder
> ls
dir1/  dir2/  file1  file2  test.txt

# List files; -l for long format, -a for all (include system, hidden files)
> ls -la
total 0
drwxr-xr-x 1 Thu 197121 0 Jan 28 17:00 dir1/
drwxr-xr-x 1 Thu 197121 0 Jan 28 17:00 dir2/
-rw-r--r-- 1 Thu 197121 0 Jan 28 17:00 file1
-rw-r--r-- 1 Thu 197121 0 Jan 28 17:00 file2
-rw-r--r-- 1 Thu 197121 0 Jan 28 17:00 test.txt

# Find files, folder in current (.) and sub-folder matching specified name pattern
> find . -name "file*"
./file1
./file2

# Find files, match specified pattern file name, case insensitive
> find . -iname "file*"
./file1
./file2
./File3

# Use grep to filter result from other command

# grep filter output that include string "fil"
> find . | grep fil
./file1
./file2

# grep filter output that strat with string "fil" OR "dir"
> find . | grep -i "fil*\|dir*"
./dir1
./dir2
./file1
./file2
./File3
Enter fullscreen mode Exit fullscreen mode

Top comments (0)