Difference between revisions of "BASH Directories"

From Indie IT Wiki
imported>Plittlefield
 
 
Line 28: Line 28:
  
 
Thanks to [http://stackoverflow.com/questions/4073969/copy-folder-structure-sans-files-from-one-location-to-another Stackoverflow.com].
 
Thanks to [http://stackoverflow.com/questions/4073969/copy-folder-structure-sans-files-from-one-location-to-another 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 [https://unix.stackexchange.com/questions/139518/create-directory-using-filenames-and-move-the-files-to-its-repective-folder Stack Exchange].
  
 
== Show Size Of Directories ==
 
== Show Size Of Directories ==

Latest revision as of 09:23, 29 November 2024

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/