Create new directory using contents of file - linux

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.

Related

Scripting file creation with specific extension

I am trying to do some basic scripting in linux (I am a recent transfer from windows) and I am simply trying to open a directory, create either the .odt or .odp files and then open them in their default programs.
I have tried to use "cat > filename.odt" but then i dont know how to stop the writing processes and proceed to next command.
#!/bin/bash
read -p "What would you like the file name to be: " name
cat > "$name".odt
xdg-open "$name.odt"
I want to just create the odt or odp file and then open it in either of their libre programs.
If the file is supposed to be blank when you create it you can just use: touch "$name".odt rather than cat. Also you don't need the quotes around the .odt in your last line. Your new file would look like this:
#!/bin/bash
read -p "What would you like the file name to be: " name
touch "$name".odt
xdg-open "$name".odt

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

Bash - cat in a hidden . file or write to it

Let's say I make a file .history.txt:
touch .history.txt
and I try to write to it:
cat > .history.txt
after having done that all I get is:
bash: .history.txt: is a directory
What I need is to be able to write some text to it like I would be able to any normal file. Any ideas what am I doing wrong?
A file doesn't need to already exist in order to redirect output to it (the shell will create the file if necessary). But Bash is telling you that .history.txt already exists and is a directory, so you can't write to it.
You either need to remove the existing directory rm -rf .history.txt or use a different file name. Then cat > .whatever.txt should work on its own.

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

Linux bash script to copy files

I need script to copy on cron basis a list of files. Files selected on name/datetime pattern and to name of file destination must by appended data like ddmmyyy.
It is not problem copy files or directory, but problem to change name of each file according to its data. May be exists some open source solution?
Thanks.
You haven't provided enough information for me to give you real working code; but you can do something like this:
file=dated_log.log
ddmmyyyy=$(read -r < "$file" ; echo "${REPLY:1:8}")
cp "$file" "$file.$ddmmyyyy"
The above will copy dated_log.log to data_log.log.30102011, assuming that the first line of dated_log.log starts with 30102011.
The Bash Reference Manual will hopefully help you adjust the above to suit your needs.

Resources