I would like to create a folder which location is available in abc.csv file. abc.scv content the following fields id,path and file name we should read the file name and move the file in the path directory if directory is not available we should create then move the directory i have try simply this

    echo "Enter directory name"
read dirname

    if [ ! -d "$dirname" ]
        echo "File doesn't exist. Creating now"
        mkdir "/home/test"/$dirname
        echo "File created"
        echo "File exists"
  • Please edit your question and provide some example input lines. – pLumo Sep 10 at 8:35
  • So you have a file with a list of paths + filenames (please give a example of this) and you want to move a file in there to the directory specified in there? How do you know where to copy them from? Are all the files in the same source directory ? – Smock Sep 10 at 10:05
  • yes the abc.scv and xyz.pdf file is avalable in /tmp directory in abc.scv having values like 1 | /var/html/test/default | xyz.pdf now we have to create directory as given location and finally move the file /var/html/test/default/xyz.pdf – Abhinaw Anand Sep 10 at 10:43

Just a short test on my side, i hope the comments explain what i have done. I'm assuming bash.

Basically googling for "bash read csv file" did the job for me. The rest is really just some if else logic and basic commands.

#!/usr/bin/env bash

OLDIFS=$IFS  # store old delimiter for csv files
IFS=" | "    # set delimiter of csv file

# attempt to get the filename of the only csv file in the current folder
# doesn't work if there are multiple csv files -> look into fuzzy finder or demenu for example
# doesn't work if csv file doesn't exist in current directory
# of course you can read the filename from command line with read command
# ls | grep *.csv might be useful to get all names of csv files in current directory
filename=$(ls | grep *.csv)  # store output of command pipe in filename
echo "$filename"  # debug, to see what is stored in $filename

while read f1 f2 f3  # read csv file, each line, 3 fields per line
    # debug, echo each field
    echo "field 1 is: $f1"
    echo "field 2 is: $f2"
    echo "field 3 is: $f3"

    echo ""
    echo "does $f2 exist?"
    [ -d $f2 ] && echo "yes" || echo "no"  # really just a short if else, in case you don't know this one

    if [ ! -d $f2 ]; then  # if directory in second column of csv doesn't exist
        echo "creating directory"
        mkdir -p $f2  # -p creates necessary parent directories if they don't exist

    echo "copying $f3 to $f2/$f3"
    cp $f3 $f2/$f3  # copying doesn' force you to move files for testing, replace with move

done < $filename

# script never reaches this point if anything before fails
# if you want to use this script more often you should think about a way to reliably reset $IFS
IFS=$OLDIFS  # reset delimiter

My csv file:

whatever number | /existing_path/nonexisting_path/move_stuff_here | testfile.txt

testfile.txt has to exist in your current working directory for this to work.

Nonexisting_path is just there to show mkdir -p.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.