1

Sometimes, we note that we have unused nested containers (directories) .

For example :

mkdir -p ~/grandpa/pa;

And pa directory is the only file existed on grandpa.

Hence, all files of pa should be moved to grandpa, then pa will be deleted.

My question is there is a built-in command line do such a mission:

i.e:

orphan  ~/grandpa/pa/*

or

fatherless ~/grandpa/pa/*
1
  • Good question. Happens to me if say I make a subdirectory to extract an archive and then the extraction software creates yet another subdirectory underneath that. – WinEunuuchs2Unix Dec 6 '19 at 1:37
4

You can just use standard bash commands - mv, rm, etc - for example

mv ~/grandpa/pa/* ~/grandpa/ && rmdir ~/grandpa/pa/

This:

  • uses mv to move the files in ~/grandpa/pa/* to ~/grandpa/
  • uses && so the rmdir command only if the mv exits successfully - ; could be used..
  • uses rmdir to remove the pa directory if it is empty.

N.B. tested on the directory structure - before:

$ tree ~/grandpa/
./grandpa/
|-- anotherfile.txt
`-- pa
    |-- child
    |   `-- file3.txt
    |-- file1.txt
    `-- file2.txt

2 directories, 4 files

after:

$ tree ~/grandpa/
./grandpa/
|-- anotherfile.txt
|-- child
|   `-- file3.txt
|-- file1.txt
`-- file2.txt

1 directory, 4 files
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.