Search code examples
linuxfilefindxargsrm

Linux: How to delete all of the files (not directories) inside a directory itself (not childs)


There are some files in a directory whose names is not usual (E.g. in unicode format). How to delete them?


Solution


  • First, find the files and then delete them:

    find [dir_path] -maxdepth 1 -type f  | xargs rm -rf
    

    Above is simple and not works when there is a space in any of file name(s). So, I've written a complex and complete command to handle spaces also:

    find ./ -maxdepth 1 -type f  | awk -F '/' '{printf "'\''%s'\''\n",$2}' | xargs rm -rf
    

    "-maxdepth 1" means just from the directory not childs. In the other means, not recursive find. As you know, "xargs" executes a following command on the list sent to it.