Replace all content of file with the content of another file - linux

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

Related

Is there a way add a file to a .zip with the file content taking from stdin?

zip has -# which takes file names from stdin. I just want to add one file with the content taken from stdin. The filename stored in the zip file should be specified in the command line. I don't think that this is possible with the zip command line. Could anybody confirm if this is the case? Thanks.

How to rename files according to other files?

I have several files in a folder with extension .img and I have only one file with extension data.txt
What I need is to copy data.txt and rename it as the names of the .img files.
For instance for the first file in my folder:
`Meaurmen_2154_data.img` >>> copy data.txt >>> rename it Meaurmen_2154_data.txt
Now I have :
Meaurmen_2154_data.img
Meaurmen_2154_data.txt ## the content is the same as data.txt
and do the same for all other files. The content of he text files will be the same for all files just we change the name according to the .img files in my folder.
Run this script
#!/bin/bash
imageFiles=( *.img );
for i in ${imageFiles[*]}
do
withoutExtension=${i%.img};
cp data.txt "$withoutExtension.txt";
done
inside the relevant directory and it will do it for you.
Try
for i in *.img; do cp data.txt $i.txt; done
rename 's/.img.txt/.txt/' *.img.txt
In some distro's rename is different, requiring
rename .img.txt .txt *.img.txt
As always, you might find yourself in need of installing additional packages.

linux add a file to an existing file

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

Create new directory using contents of file

In bash I have a file called temporary.txt that contains one line of text. I want to use the contents of this line of text to create a new directory? How can i do this?
Try this:
mkdir "$(< temporary.txt)"
Try this which is less complex than the accepted answer.
mkdir `cat temp`
For each and every word in your text file this creates a directory.

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