GʀᴜᴍᴘʏCᴀᴛ
GʀᴜᴍᴘʏCᴀᴛ

Reputation: 8920

Add a file in a folder with bash

I have figured out how to remove bad files in my folders but I am wanting to know how to add certain named files to that folder.

I want to add something like address.xml

I have this and can remove the bad files.

    for addxml in $(find $DIRECTORY/$directoryname -name address.xml); do
    rm -v $addxml
done

I am trying to learn how later down the code I can add a file from another folder no where near the folders that are being edited.

Upvotes: 0

Views: 2230

Answers (5)

V H
V H

Reputation: 8587

the question and the script does not match in the sense the script is looking for an existing file and the request states how to put a file (i.e. non existant file in to the location)

#!/bin/sh

FILECONTENT="something";
FILECONTENT=$FILECONTENT"something2";


for addxml in $(find . -name address.xml); do

if [ $? = 0 ]; then
        echo found
        echo   rm -v $addxml
else
        echo "putting content in $addxml"
        echo $FILECONTENT > $addxml
fi      
done

# OR CHANGE IT TO 
#FILECONTENT="/tmp/filecontent"
#cat $FILECONTENT > $addxml

Upvotes: 0

Justin M. Keyes
Justin M. Keyes

Reputation: 6964

how later down the code I can add a file from another folder

OTHER_DIRECTORY=/home/CuriousGeorge/some/other/directory

for addxml in $(find $DIRECTORY/$directoryname -name address.xml); do
    mv $addxml $OTHER_DIRECTORY
done

Upvotes: 0

ILikeTurtles
ILikeTurtles

Reputation: 1022

vim address.xml ...?

:wq

Are you creating a bash scrip to make files or you are in a command prompt and need to make a file?

Upvotes: 0

Rob Allen
Rob Allen

Reputation: 17719

If you want to create an empty file, use touch /<dir>/<filename.ext> (docs). If you're trying to move a file from one place to another, use mv <source> <target> (docs)

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272297

I would suggest checking out cp (to copy files/directories) or mv (to move files/directories)

Upvotes: 2

Related Questions