Results 1 to 3 of 3
Hi, (I posted this in Ubuntu help area before I realised there was a scripting area, apologies to mods - please delete the other one, thanks!)
I had a corrupt ...
- 06-04-2008 #1Just Joined!
- Join Date
- Mar 2008
- Posts
- 7
[SOLVED] Bash Script help! moving files
Hi, (I posted this in Ubuntu help area before I realised there was a scripting area, apologies to mods - please delete the other one, thanks!)
I had a corrupt file system which I was able to use some tools on and recover data. The problem is that 900+ directories were created randomly with sequenced filenames.
So I've created a bash script to basically create directories based on file extensions then move the files into their sorted directories which will make it easier for me to scan.
My script is below, currently what it's doing is creating the directories eg.
/home/fitzwesley/gz
/home/fitzwesley/html
/home/fitzwesley/txt
But the files are all being moved into the last directory eg. /home/fitzwesley/txt, I know it's something simple, any chance of some help?
Code:#!/bin/bash FILEDIR=/home/fitzwesley # List file extensions and create directories echo Files found in $( pwd ) for EXTENSIONS in $( ls -a | grep -v ^d | grep [.][a-zA-Z0-9] | cut -d. -f2 ); do if [ -d $FILEDIR/$EXTENSIONS ]; then echo "$EXTENSIONS found in $FILEDIR, not created" else mkdir -p $FILEDIR/$EXTENSIONS echo "Directory created: $EXTENSIONS" fi # Start moving files into dirs echo Moving files into directories... for FILES in $( ls -a | grep -v ^d | grep [.][a-zA-Z0-9] ); do i=0 if [ -e $FILEDIR/$EXTENSIONS/$FILES ]; then echo "File $FILES already exists, renaming..." i=$[$i+1] #$FILES=$i.$FILES mv $FILES $i.$FILES else mv $FILES $FILEDIR/$EXTENSIONS echo "$FILES moved to $FILEDIR/$EXTENSIONS" fi done done
- 06-04-2008 #2
I think you have nested your second for loop when you meant to have 2 stand-alone ones. What your script does is creates a directory with each extension type as it's name and then moves all the files into it. As txt is the last extension it comes across all the files end up in there.
What I think you're after is something like (I haven't tested this so there may be a bug somewhere):
Code:#!/bin/bash FILEDIR=/home/fitzwesley # List file extensions and create directories echo Files found in $( pwd ) for EXTENSIONS in $( ls -a | grep -v ^d | grep [.][a-zA-Z0-9] | cut -d. -f2 ); do if [ -d $FILEDIR/$EXTENSIONS ]; then echo "$EXTENSIONS found in $FILEDIR, not created" else mkdir -p $FILEDIR/$EXTENSIONS echo "Directory created: $EXTENSIONS" fi done # Start moving files into dirs echo Moving files into directories... for FILES in $( ls -a | grep -v ^d | grep [.][a-zA-Z0-9] ); do # Find the file's extention EXTENSION=$( ls $FILES | cut -d. -f2 ) i=0 if [ -e $FILEDIR/$EXTENSION/$FILES ]; then echo "File $FILES already exists, renaming..." i=$[$i+1] #$FILES=$i.$FILES mv $FILES $i.$FILES else mv $FILES $FILEDIR/$EXTENSION echo "$FILES moved to $FILEDIR/$EXTENSION" fi done
Linux User #453176
- 06-04-2008 #3Just Joined!
- Join Date
- Mar 2008
- Posts
- 7
Thanks Kieren,
That worked perfectly! I had started out with two separate loops but I needed to find the extension again:
Saved me some major agony.Code:EXTENSION=$( ls $FILES | cut -d. -f2 )


