linux add a file to an existing file - linux

I have a bash script that runs through several folders and processes the files' content. The resulting file, newFile I want to append to a file in another folder. This file has the same name (newFile). So, I do this
cat $outfn1 >> /newFolder/newFile
But this does not work. The newFile is surely moved to the newFolder. However, it is not appended to the old version of the newFile, which is what I would like it to do. It replaces the old version.
Is there any way around this one?
Thank you.
jd

Related

How can I copy multiple files in the same directory with different names but same extension in bash?

The requirement is to make copies of n(n>10000) files in the same linux directory.
The extension of the files has to be intact and can probably add numbers to distinguish among files.
For e.g. if one file is text1.txt the other could be text2.txt
But I have to create multiple copies from multiple files and not from a single file.
Please help.
Bash pattern substitution might help you here. If you e.g. want to copy all .txt files, you can do it like this:
for file in *.txt # add any other name wildcards
do
filename=${file%.*} # removes everything after the last dot
extension=${file##*.} # removes everything before the last dot
cp "$file" "${filename}-copy.${extension}" # adds the -copy suffix to every copy
done
You might want to look into tools like logrotate which can take for example a glob and rotate each of the files on a regular basis.

Changing a file extension using rename in OS X

I have a lots of file names under a folder '/files' ,the extesnions of these files are appended with timestamp when that file was created,somthing like abc.csv_20170329. I want to change the extension of all these files to abc.csv_20170330 using the rename command in OSX using the terminal.Can any one help me with the exact command to do that i tried using
$ rename -S '.csv_' .csv_20170330 '.csv'
but this does not work.
for file in abc.csv*
do
mv "$file" "$file__20170330"
done

Replace all content of file with the content of another file

Is there a way to replace all content of a file with the content of another file without destroying the first file?
If I have a file in the current directory called myfile.txt, if I do the following:
cp updatedfiles/myfile.txt myfile.txt
then my old file is deleted and replaced with a new file (with the same name). I do not want to delete the existing file, I just want to change the content of the file. How to do this?
cat updatedfiles/myfile.txt > myfile.txt

Move files to different directories based on file name tokens

I am looking to write a script to move files from a directory:
/home/mydir/
To another directory based on tokens in the file name. I have a bunch of files named as such:
red_office_mike_2015_montreal_546968.ext
or
$color_$location_$name_$year_$city_$numbers.extension (files will be various movie files: mov, mp4, mkv, etc.)
I would like the script to move the files to the following location:
/dir/work/$color/$name
Then verify the file has successfully copied, and delete the original file once it has.
I would also love it if the script would create the to directory if it does not already exist.
So in summary, I need a script to move files based on underscore separated tokens, create the to directory if it doesn't already exist, verify the successful copy (maybe with a size check), then delete the original file.
I am working on linux, and would prefer a bash script. The variables I have given are generic, and I will incorporate some other things to the script, I'm just looking for help on building the skeleton.
Thanks in advance for any help!
It's not a bash script, but perl is much better at this kind of thing and is installed on all Linux systems
while(<>) {
chomp;
$file = $_;
($colour, $location, $name, $year, $city, $numbers) = split(/_/,$file);
$dest0 = "/dir/work/$colour";
$dest1 = "$dest0/$name";
mkdir ($dest0) unless (-d $dest0);
mkdir ($dest1) unless (-d $dest1);
rename ($file, "$dest1/$file");
}
The script splits your input file on the underscore character, creates all the directories to the destination and then renames the file to the new filename. Rename takes care of all the copying and deleting for you. In fact it just changes the directory entries without any copying at all.
UPDATE
The above version takes its input from a file containing a list of filenames to process. For an alternative version which processes all files in the current directory, replace the while line with
while(glob("*")) {
I was able to fumble around online and come up with a for loop to do this task. I used cut and it made things simple. Here is what worked for me:
#!/bin/sh
cd "${1:-.}"
for f in *.*; do
color=`echo "$f" | cut -d'_' -f1`
name=`echo "$f" | cut -d'_' -f3`
todir="/dir/work/$color/$name"
mkdir -p "$todir"
mv "$f" "$todir"
done
This worked perfectly and I hope it can help others who might need to create directories based on portions of filenames.
The first line under the shebang made it so that it will either look at the current working directory or a directory you pass it as an argument.
Thanks to those who chimed in on the original post. I'm new with scripting so it take me a while to figure this stuff out. I love this site though, it is super helpful!

Bash script to append dynamic file path to existing .ini file possibly using sed?

i have a commonly named .sqlite file contained within many unique user's home folders with file structure: /home/user/unique-ip-address/folder/file.sqlite I've decided to move all of these .sqlite files to a tmpfs mount and have already done so maintaining the full directory structure, so each .sqlite file is now in: /mnt/tmpfs/home/user/unique-ip-address/folder/file.sqlite
I'm looking for a way to edit a commonly named .ini file in each users home folder with a unique file path (ex: /home/user/unique-ip-address/folder/file.ini), i figure using find and sed should do the trick but i'm not sure how as the find results need to match the user to the correct folder in /mnt/tmpfs. I would like to append the new .sqlite location used in /dev/shm to file.ini in their home folders after dir= in the file.ini. Thanks!
I believe something like this should do the trick:
find /home/*/unique-ip-address/folder/file.ini | xargs -n1 perl -pe 'BEGIN{$infile="/mnt/tmpfs/$ARGV[0]"} {s/dir=/$&$infile/}'
That command doesn't change anything, it just prints out everything. If it looks like it does what you want, just add a -i.bak to the very end and it will make the changes in place. The original file will be renamed to file.ini.bak.
find /home/*/unique-ip-address/folder/file.ini | xargs -n1 perl -pe 'BEGIN{$infile="/mnt/tmpfs/$ARGV[0]"} {s/dir=/$&$infile/} -i.bak'

Resources