简体   繁体   中英

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.

我建议检查出cp (复制文件/目录)或mv (移动文件/目录)

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 )

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?

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

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 (ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM