BASH Directories

From Indie IT Wiki
Revision as of 09:23, 29 November 2024 by Gcoles (talk | contribs) (→‎Reproduce Directory Structure Sans Contents)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Delete Only Directories

find . -maxdepth 1 -mindepth 1 -type d -exec rm -rf '{}' \;

Create Multiple

mkdir {01..12}

List Directories By Last Modified Date

ls -t

Of if you want to do it in reverse:

ls -tr

Reproduce Directory Structure Sans Contents

Copy the file structure:

find . -type d -print0 >dirs.txt and 

Reproduce the file structure:

xargs -0 mkdir -p <dirs.txt

This will work for folder structures that have spaces in the names.

Thanks to Stackoverflow.com.

Reproduce Directory Structure Sans Contents, Then Move Contents In To Corresponding Directories

for file in *.mp3; do mkdir -- "${file%.mp3}"; mv -- "$file" "${file%.mp3}"; done # Where .mp3 represents your file type

Works with files that have spaces in the names.

Thanks to Stack Exchange.

Show Size Of Directories

Sorted by time...

du --time -s */ |sort -k 2

Sorted by size..

du --time -s */ |sort -k 1 -h

Test If A Directory Exists

test -d /path/to/directory

You can use this to make sure a directory exists before copying or moving a file into it.

test -d /mnt/usb/backup && mv /home/website/backup-20140901.zip /mnt/usb/backup/