directories and file arrangement in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting directories and file arrangement in bash
# 1  
Old 12-09-2011
directories and file arrangement in bash

im trying to write a script that will put files with different extensions into their specified directories

In the directory are files of various types, i want to arrange the files on individual directories under their type. There are three distinct types of files: 1) Text documents - files with extensions. Doc,. Txt,. Pdf, ... 2) multimedia files - with the extensions. Mpg,. Avi,. Mp3 ... 3) graphics files - with the extensions. Jpg,. Gif,. Png ... 4) All other files Files types 1-3 to move into individual directories and files of other types have to move in directories with names corresponding to the extension (you can bring them to uppercase - BAK, CPP ,...). i want the script to run with the following parameters: 1)-m path - if specified, the multimedia files will be moved to the directory PATH 2)-d PATH - specifies the path where you have to move your documents 3)-l indicates that when the file names should lead to lower case 4)-x indicates that they move to lower case file extension any ideas ??i am a newbie and im trying to learn bash scripting so i thought of this question
# 2  
Old 12-10-2011
And what have you tried so far?
# 3  
Old 12-12-2011
Since you are a self-professed "newbie", I will add commentary to the script.
Code:
#! /bin/bash

NAME=$(basename ${0})

declare -A extlist

extlist[d]="${FILEREXTLIST_D:-csv:doc:docx:html:log:odt:pdf:rtf:sxw:tex:txt:wpd:wps:wri:xml${FILEREXT_D:+:${FILEREXT_D}}}"
extlist[i]="${FILEREXTLIST_I:-bmp:gif:jpe:jpeg:jpg:mmg:pbm:png:ppm:tif:tiff:xbm:xcf${FILEREXT_I:+:${FILEREXT_I}}}"
extlist[m]="${FILEREXTLIST_M:-aiff:asf:au:avi:m1v:m2v:m4a:mp2:mp3:mp4:mpeg:mpg:wav:wma:wmv${FILEREXT_M:+:${FILEREXT_M}}}"
extlist[o]=

Build the list of extensions for each filetype group: "d" for document, "i" for image, "m" for multimedia, and "o" for other. You can override the default list by setting the FILEREXTLIST_X environmental variable to a colon separated list of extensions or you can add to the default list by setting FILEREXT_X environmental variable to a colon separated list of extensions.
Code:
declare -A exttype
exttype[d]=document
exttype[i]=image
exttype[m]=multimedia
exttype[o]=other

For an error message, see below
Code:
declare -A extdir
extdir[d]="${FILERDIR_D}"
extdir[i]="${FILERDIR_I}"
extdir[m]="${FILERDIR_M}"
extdir[o]="${FILERDIR_O}"

Sets the default directories for each filetype group.
Code:
declare -A extmap

The extmap array will be used to map extensions to directories -- it will be populated below.
Code:
#-------------------------------------------------------------------------------

while getopts :a:A:D:d:I:i:M:m:o:O:x:nv OPT; do
    case "${OPT}" in

        a)  mapall=',,' ;; # to lower
        A)  mapall='^^' ;; # to upper

On of your requirements is:
Quote:
... other types have to move in directories with names corresponding to the extension (you can bring them to uppercase - BAK, CPP ,...).
I have to admit that having this be the default option is a recipe for sadness, imho you should not move files only if requested. So the -a maps files with 'other' extension to a directory with the extension in all lower case, -A maps to upper case.
Code:
        [IDMO]) # ext
            if [[ -z "${OPTARG}" ]]; then
                echo "${NAME}:" missing extention for "-${OPT}" 1>&2
                exit 1
            fi

            x="${OPT,,}"
            extlist[${x}]="${extlist[${x}]}:${OPTARG,,}"
            ;;

The -X ext option allows you to add to the extension list for each filetype group
Code:
        [idmo]) # dir
            if [[ -z "${OPTARG}" ]]; then
                echo "${NAME}:" missing directory for "-${OPT}" 1>&2
                exit 1
            fi

            if [[ ! -d "${OPTARG}" ]]; then
                echo "${NAME}:" not a directory - "${OPTARG}" 1>&2
                exit 1
            fi

            extdir[${OPT}]="${OPTARG}"
            ;;

The -x dir option allows you to set the directory for each filetype group
Code:
        x) # ext[:ext]...=dir
            if [[ "${OPTARG}" != *=* ]]; then
                echo "${NAME}:" invalid mapping - "${OPTARG}" 1>&2
                exit 1
            fi

            dir="${OPTARG#*=}"

            if [[ ! -d "${dir}" ]]; then
                echo "${NAME}:" not a directory - "${dir}" 1>&2
                exit 1
            fi

            for ext in ${OPTARG%%=*}; do
                extmap[${ext}]="${dir}"
            done
            ;;

Use the -x=ext=directory to explicitly map an extension to a specified directory. Multiple colon separated extensions may be specified.
Code:
        n)  verbose=-nv ;; # no action
        v)  verbose=-x  ;; # verbose

        \?)
            sed -e 's/^                //' <<eof 1>&2
                Usage

                    ${NAME} [file...]

                Description
                .
                .
eof
            ;;

I leave filling out the help function up to you Smilie
Code:
    esac
done

shift $(( OPTIND - 1 ))

#-------------------------------------------------------------------------------

IFS="${IFS}:"

for x in d i m o; do
    if [[ -z "${extdir[${x}]}" ]]; then
        echo ${NAME}: no "${exttype[${x}]}" directory specified 1>&2
        exit 1
    fi

    for ext in ${extlist[${x}]}; do
        if [[ -z "${extmap[${ext}]}" ]]; then
            extmap[${ext}]="${extdir[${x}]}"
        else
            echo ${NAME}: duplicate extension mapping - "${ext}" 1>&2
        fi
    done
done

IFS="${IFS%:}"

Map all the extensions of each filetype group to their directories. The spliting apart the list of extensions was done by appending a colon to IDS, which lets the shell do the hard work.
Code:
#===============================================================================

for file in "${@}"; do
    if [[ ! -e "${file}" ]]; then
        echo "${file}:" no such file or directory 1>&2
        continue
    fi

    if [[ ! -f "${file}" ]]; then
        echo "${file}:" not a file 1>&2
        continue
    fi

    ext="${file##*.}"

    if [[ "${file}" == "${ext}" ]]; then
        echo "${file}:" has no extension 1>&2
        continue
    fi

    if [[ -z "${ext}" ]]; then
        echo "${file}:" missing extension 1>&2
        continue
    fi

    dir="${extmap[${ext}]}"

    if [[ -z "${dir}" ]]; then
        if [[ -n "${mapall}"  ]]; then
            eval dir="\${ext${mapall}}"
        else
            if [[ -n "${verbose}" ]]; then
                echo "${file}:" no directory mapping 1>&2
            fi
            continue
        fi
    fi

    if [[ ! -d "${dir}" ]]; then
        echo "${file}:" missing directory - "${dir}" 1>&2
        continue
    fi

    echo mv "${file}" "${dir}"
done \
| sh -e ${verbose}

OK, how I decided to move the files may be considered a bit cheesy. The script echos out the appropriate mv commands that are subsequently sent to sh to process. This lets me sets take advantage of the -n option to print out what would be done, but not actually move files around.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to update file on prefix match in two directories

I am trying to use bash to loop through a directory /path/to/data using a prefix match from /path/to/file. That match is obtained and works using the code below (in green)... what I can not seem to do is populate or update the corresponding prefix_file.txt in /path/to/data with the values in each... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Bash to create sub directories from specific file extension

In the bash below I am trying to create sub-directories inside a directory from files with specific .bam extensions. There may be more then one $RDIR ing the directory and the .bam file(s) are trimmed (removing the extension and IonCode_0000_) and the result is the folder name that is saved in... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Merge values from multiple directories into one file in awk or bash

I am trying to merge or combine all $1 values in validation.txt from multiple directories into one new file and output it here tab-delimited:/home/cmccabe/Desktop/20x/total/total.txt. Each $2 value and the header would then be a new field in total.txt. I am not sure how to go about this as cat is... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

Chose list of sub directories in a bash script file

Hi, I have a command in my bash script, searchDirectoryName.sh: DIR_NAME=$(find . -type d) echo "${DIR_NAME}" . ./Dir1 ./Dir1/1.0.2.1 ./Dir2 ./Dir2/1.1 ./Dir3 ./Dir3/2.2.1 How can I select only following directory names with second subdirectoies and without first ./ in the... (3 Replies)
Discussion started by: hce
3 Replies

5. UNIX for Dummies Questions & Answers

Tar command to preserve the folder/file arrangement

Hi, I do have question for un tar a file. I have several 'tar'ed files. For example: SRS.tar.bz2. I was trying to untar them in a linux server using the command: tar xvjf SRS.tar.bz2 It worked perfectly. but when I open this file in my mac computer all the files are extracted into a... (7 Replies)
Discussion started by: Lucky Ali
7 Replies

6. Shell Programming and Scripting

Bash to monitor Modified Files/Directories

Hi All , I have a directory called "/usr/local/apache/docs/" inside this docs i have below directories , bash-2.05# pwd /usr/local/apache/docs/ bash-2.05#ls -l | less 2 drw-r-xr-x 3 root root 512 Aug 8 2010 Form1 2 drw-r-xr-x 3 root other 512 Mar 8 ... (4 Replies)
Discussion started by: gnanasekar_beem
4 Replies

7. Shell Programming and Scripting

Count Directories In bash

Hi ALL I have small script that find 777 dir now i want to count these directories,Keep in mind there are many directories. This IS my code below. #!/bin/bash check=/var/www/html find $check -type d -perm 777 | while read DIR do ... (2 Replies)
Discussion started by: aliahsan81
2 Replies

8. Shell Programming and Scripting

(w)get web server's directories + bash script

Hi everyone. I need to write a script which will download files/folders (a huge collection) to the local file server (centOS 4.4 Server), and check regularly (every 6 hours or so if any new files are present, or if the old ones were modified to update contents). Any insights on how to tackle... (2 Replies)
Discussion started by: reminiscent
2 Replies

9. UNIX for Dummies Questions & Answers

Help on file arrangement

Can anyone help me on this. I have a file that looks like this: color red green blue color pink yellow number one two gender male gender female The output would look like this: color red green blue pink yellow number one two gender male female I have over 5000 rows and i dont want... (5 Replies)
Discussion started by: kharen11
5 Replies

10. UNIX for Dummies Questions & Answers

Text file arrangement

Dear UNIX experts: Hi, I have a text file which the contents are arranged vertically down, line by line. How do use a loop (I think) to make it arrange in vertical arrangement with a tab delimitated and write to a new file? Eg: of source file Hello World Good-day Thanks Welcome The... (8 Replies)
Discussion started by: merry susana
8 Replies
Login or Register to Ask a Question