Results 1 to 2 of 2
I been using linux for a while, but I havent had the time to really learn it. Something Id like to get more familiar with is bash so here is ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 03-28-2011 #1
- Join Date
- Mar 2010
make a new folder for each file in a particular folder
I have a directory full of files. For each file in the directory create a new subdirectory with each files name.
I tried this:
#im already in this directory
for a in $aaa
do mkdir $a
One of the problems is that there are spaces () and - in the filenames so I get many subfolders with names like "-" which i don't want.
I tried stuff like:
for a in $aaa
do mkdir "'"$a"'"
This of course also did not work.
Additional I tried just creating a folder with the name of one of the files and I got this:
mkdir: cannot create directory `(audio) - R56339.EXE': File exists
Well yeah a file exsists with that name, but I want to create a folder- why is there a conflict?
Just to be clear here are the contents of this folder:
~/Desktop/LAB/Media/Drivers - (Image Deployment)/Optiplex_GX260/XP> ls
(audio) - R56339.EXE (NIC-Onboard-Intel) - R54402.EXE
(Modem-datafax-V92) - R54403.EXE OEM_Applications
(modem-PCI-V90) - copperXP.EXE (Video-Onboard-82845G-GL-GE-PE-GV) - R126990.EXE
(modem-soft-v92) - R54335.EXE (Video-Onboard-Intel G) - R79733.EXE
~/Desktop/LAB/Media/Drivers - (Image Deployment)/Optiplex_GX260/XP>
OEM_Applications is a folder
Thanks for any replies. All I ask is go easy on the terseness I'm a newbie here.
- 03-28-2011 #2
I am new to BASH scripting as well and trying to learn. I put the following script together to try and accomplish your task.
#!/bin/bash # Save current $IFS contents SAVEIFS=$IFS # Set new $IFS contents IFS=$(echo -en "\n\b") # Check script name name=`basename $0` # Process each file in current directory for f in *; do # Process all directories and files? if [ "$1" = "-a" ]; then mkdir -pv "./dir/$f" else # Is $f a directory? if [ -d $f ]; then echo $f is a directory. else mkdir -pv "./dir/$f" fi fi done # Clean-up directory named after script rmdir "./dir/$name" # Revert to previous $IFS contents IFS=$SAVEIFS
Note that "$IFS" is an environmental variable built into BASH. It determines how BASH recognizes field separation. In this example, the field separator is set to a new-line and a backspace. This is what causes the for loop to process a space-separated file name as a single name. Since this is an environment variable, it will effect how other BASH scripts function as well, so when the script above finishes we have it revert to how it was before we changed it.
The script will of course need to be ran from within the directory of the files to be processed. One issue is that it appears a file and a directory cannot co-exist with the same name, in the same location. Because of this, the script will create a new sub-directory called 'dir' and create all the sub-directories, based on the processed file names inside of the dir directory.
I hope this helps. Let me know if you have any questions or problems with the script.
Last edited by Nagarjuna; 03-29-2011 at 02:13 AM.