Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Removing empty folders using the "find" command Post 302178567 by ShawnMilo on Tuesday 25th of March 2008 09:31:08 PM
Old 03-25-2008
Quote:
Originally Posted by krishmaths
You can use the below command

Code:
find ./ -type d -exec rmdir 2>/dev/null {} \;

The above will remove all empty directories from the current directory.
To clarify this, the rmdir command should fail if the directory isn't empty. So by ignoring error messages (redirecting them to a black hole, /dev/null), you will successfully delete the empty ones and the non-empty ones shouldn't be in any danger.

It would probably be a good idea to put dummy data into a directory and try to use rmdir on it, just to make sure your environment behaves this way. But it's the default, so it should be fine.

ShawnMilo
 

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

check input = "empty" and "numeric"

Hi how to check input is "empty" and "numeric" in ksh? e.g: ./myscript.ksh k output show: invalid number input ./myscript.ksh output show: no input ./myscript.ksh 10 output show: input is numeric (6 Replies)
Discussion started by: geoffry
6 Replies

2. UNIX for Dummies Questions & Answers

Removing empty folders using 'find'

Hey there! I try to use 'find' to remove empty directories like this: find . -depth -type d -empty -exec rm -rf {} ';' It works just fine, but there are some directories i want to exclude. So i tried to do sth like this: find . -depth -type d -empty -exec grep -v "not this one please" -exec... (5 Replies)
Discussion started by: deTTo
5 Replies

3. Shell Programming and Scripting

"find command" to find the files in the current directories but not in the "subdir"

Dear friends, please tell me how to find the files which are existing in the current directory, but it sholud not search in the sub directories.. it is like this, current directory contains file1, file2, file3, dir1, dir2 and dir1 conatins file4, file5 and dir2 contains file6,... (9 Replies)
Discussion started by: swamymns
9 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Problem with "find" and "grep" command

I want to list all files/lines which except those which contain the pattern ' /proc/' OR ' /sys/' (mind the leading blank). In a first approach I coded: find / -exec ls -ld {} | grep -v ' /proc/| /sys/' \; > /tmp/list.txt But this doesn't work. I got an error (under Ubuntu): grep:... (5 Replies)
Discussion started by: pstein
5 Replies

6. UNIX for Dummies Questions & Answers

find/xargs/*grep: find multi-line empty "try-catch" blocks - eg, missing ; not in a commented block

How can I recursively find all files in a directory and print out the file and first line number of any text blocks that match the below cases? This would seem to involve find, xargs, *grep, regex, etc. In summary, I want to find so-called empty "try-catch blocks" that do not contain code... (0 Replies)
Discussion started by: lifechamp
0 Replies

7. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

8. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies
rmdir(1)						      General Commands Manual							  rmdir(1)

NAME
rmdir - Removes a directory SYNOPSIS
rmdir [-p] [-s] directory... STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: rmdir: XCU5.0 Refer to the standards(5) reference page for more information about industry standards and associated tags. OPTIONS
Removes all directories in a path name. For each directory argument, the directory entry it names is removed. If the directory argument includes more than one path name component, effects equivalent to the following command occur: rmdir -p $(dirname directory) That is, rmdir recursively removes each directory in the path name. OPERANDS
The path name of an empty directory to be removed. DESCRIPTION
The rmdir command removes a directory from the system. The directory must be empty before you can remove it, and you must have write per- mission in its parent directory. Use the ls -al command to see if a directory is empty. If a directory and a subdirectory of that directory are specified in a single invocation of rmdir, the subdirectory must be specified before the parent directory so that the parent directory will be empty when rmdir tries to remove it. RESTRICTIONS
A directory must be empty before you can remove it, and you must have write permission in its parent directory. If the -p option is used, all directories in the path must be empty except for the directory being recursively removed. EXIT STATUS
The following exit values are returned: Each directory specified by directory operand was successfully removed. An error occurred. EXAMPLES
To empty and remove a directory, enter: rm mydir/* mydir/.* rmdir mydir This removes the contents of mydir, then removes the empty directory. The rm command displays an error message about trying to remove the directories . (dot) and .. (dot dot), and then rmdir removes them. Note that rm mydir/* mydir/.* first removes files with names that do not begin with a (dot), then those with names that do begin with a (dot). You may not realize that the directory contains file names that begin with a (dot) because the ls command does not normally list them unless you use the -a option to see the files whose names begin with a (dot). To remove all of the directories in the path name a/b/c, enter: rmdir -p a/b/c Use a command like this one if directory a in the current directory is empty except that it contains a directory b and a/b is empty except that it contains a directory c. ENVIRONMENT VARIABLES
The following environment variables affect the execution of rmdir: Provides a default value for the internationalization variables that are unset or null. If LANG is unset or null, the corresponding value from the default locale is used. If any of the internationalization vari- ables contain an invalid setting, the utility behaves as if none of the variables had been defined. If set to a non-empty string value, overrides the values of all the other internationalization variables. Determines the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multibyte characters in arguments). Determines the locale for the for- mat and contents of diagnostic messages written to standard error. Determines the location of message catalogues for the processing of LC_MESSAGES. SEE ALSO
Commands: mkdir(1), ls(1), rm(1) Functions: rmdir(2), unlink(2), remove(3) Standards: standards(5) rmdir(1)
All times are GMT -4. The time now is 08:26 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy