Remove only files in directory on linux NOT directories
What worked for me is a PERL script:
perl -e 'chdir "subdirectory_name" or die; opendir D, "."; while ($n = readdir D) { unlink $n }'
Run this one level up from the directory you wish to clean: replace "subdirectory_name" with the directories name.
Worked on millions of files without killing the CPU.
Linux: How to delete all of the files (not directories) inside a directory itself (not childs)
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.
Remove only files and not the directory in linux
Try this:
rm /path/to/directory1/*
by adding the -r
option you can additionally remove contained directories and their content recursively.
Delete contents but not directory in Linux?
If "dir" is the directory, give the commands,
cd dir
rm -r *
How to delete all files except directories?
find . -maxdepth 1 -type f -print0 | xargs -0 rm
The find
command recursively searches a directory for files and folders that match the specified expressions.
-maxdepth 1
will only search the current level (when used with.
or the top level when a directory is used instead), effectively turning of the recursive search feature-type f
specifies only files, and all files
@chepner recommended an improvement on the above to simply use
find . -maxdepth 1 -type f -delete
Not sure why I didn't think of it in the first place but anyway.
What is the command to remove files in Linux from a particular directory which are owned by a particular user?
find /home/username -maxdepth 1 -type f -user "dev-user" -delete
Use the user flag to specify files owner by a specific user and use -delete to remove the files.
Set maxdepth 1 to search for files within /home/username only and not child directories.
How to list only files and not directories of a directory Bash?
Using find
:
find . -maxdepth 1 -type f
Using the -maxdepth 1
option ensures that you only look in the current directory (or, if you replace the .
with some path, that directory). If you want a full recursive listing of all files in that and subdirectories, just remove that option.
Related Topics
What Is the Recommended Way to Perform Source-Level Debugging of System Library Calls
Rename Part of File Name Based on Exact Match in Contents of Another File
Linux Script to Automate Ftp Operation
If I Have a Process, and I Clone It, Is the Pid the Same
String Comparison Not Working Properly
While Do Loop and Variables in a Bash Script
Python Socket.Error: [Errno 13] Permission Denied
Insert New Line to Bash Prompts
Splitting Gzipped Logfiles Without Storing the Ungzipped Splits on Disk
Execute Command Line and Return Command Output
Xdp Program Ip Link Error: Prog Section Rejected: Operation Not Permitted
Git Sparse-Checkout Ignore Specific File Type
G++ Searches /Lib/../Lib/, Then /Lib/
How to Use Code Completion into Eclipse with Opencv
"Couldn't Find a File Descriptor Referring to the Console" on Ubuntu Bash on Windows