I want to write a shell script that will traverse the directory structure starting from a directory c, where c is given as the input argument for your traversal script.

  • During traversal, I want each directory I have visited to be printed out on my screen.
  • Along with the directories, I also want the subdirectories, the list of files, and their sizes printed out.
  • Files that are greater than 1KB should be printed out.

I can only list files in a current directory but I cannot traverse through them.

This printed all the files in my directory Sample including the sizes and permissions. This is not what I want because I want to print directories, sub-directories, and files from any folder using the directories as input not just in a specific directory.


for Sample in "$/Sample"/*
  ls -l
  #echo "$Sample"

#All this did was to print all the directories in my system. This is not what I want

for d in "$dir"/*
  echo "$d"

#What I am working on now but it is giving me syntax errors near done. -d $file #prints out for directory, while -f $file print out files.

for file in ${dir}/*; do
    if [[ -d $file ]]; then
        echo "$file:"
    if [[ -f $file ]]; then
        echo "$file"
  • Welcome, please share what you have tried. Commented Feb 1, 2021 at 15:40
  • If the solutions you found on the internet cover 80% of what you want, it should not be too hard to adapt the solutions so they cover the rest as well. To start from scratch, your solution should be centered on the find command. It's fundamental purpose is to visit all files in a directory tree, and it has numerous filters (size, file type, permissions and so on) as well as actions on the files thus found, such as the exec option. Commented Feb 1, 2021 at 16:11

2 Answers 2


Your requirements can be satisfied by a single find command:

$ find path/to/dir \( -type d -o \( -type f -a -size +1024k \) \) -printf '%s %p\n'

This says:

  • Select directories (-type d)
  • OR (-o)
  • ( Select files (-type f) AND (-a) larger than 1024KiB (-size +1024k) )
  • Do a formatted print (-printf) of the file size in bytes, the file path and a newline (%s %p\n)

The parentheses are used for grouping but must be escaped with backslashes to prevent them being interpreted by the shell.

Further explanations are in man find.

This prints files that are larger than 1024k, not exactly 1024k.

  • Note that -printf and that k suffix are GNU extensions (the latter is now found in some other implementations). Commented Feb 1, 2021 at 16:21
  • Fair enough - I didn't mention it as OP tagged this Ubuntu so will be using GNU find. But maybe the casual browser won't notice that tag.
    – grifferz
    Commented Feb 1, 2021 at 16:25
  • It works but I want my terminal to accept user inputs. Do you have an idea on how I'll do that? what is the command for that?
    – web nerd
    Commented Feb 1, 2021 at 18:41
  • @StéphaneChazelas Nevermind! I have figured it out! Thank you so much! if only I can upvote your solution twice!
    – web nerd
    Commented Feb 1, 2021 at 19:09

bash is not the best choice of shell for that. With zsh:

#! /bin/zsh -

zmodload zsh/stat || exit

(( $# != 0 )) || set .

(( $#files == 0 )) ||
  stat -Ln +size -- $files

Here LK+1 selects files (regular files only with .) whose size is strictly greater than 1KiB (1024 bytes). For files greater than 1KB (1000 bytes), replace it with L+1000.

  • I am using Ubuntu. Is there a different syntax for it.
    – web nerd
    Commented Feb 1, 2021 at 16:27
  • @webnerd, which Unix-like OS you use is not very relevant here. zsh like bash predates Ubuntu and even Linux, but you need it to be installed. On Ubuntu, you (or the administrator) would need to run apt install zsh zsh-doc as root if zsh is not already installed. Commented Feb 1, 2021 at 16:30
  • It gave me errors like zmodload: command not found. Why do you think this is?
    – web nerd
    Commented Feb 1, 2021 at 16:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .