open a folder from a name in txt file BASH - bash

I have a text file fold.txt that contains one line fold_nam:
$ cat fold.txt
fold_nam
This name in the the text file is an output that was created during a program run of a folder's name that now contains other files that I need to work with.
I am writing a big script and now I need to enter this folder and I need to get the name from the text file. I tried several things but cannot really work it out.

There's no need to use cat:
cd $(<fold.txt)

If you want to read the line into a variable: read -r folder_name < fold.txt

You should be able to do this:
cd $(cat fold.txt)
or
cd `cat fold.txt`

Related

Renaming files from a list using a shell script

I would like to rename files from a list. This is the file format:
Original name
New name
000000000000402a.ogg
Dth_BrkNck_FOD_MALE_MALE_001
000000000000402c.ogg
Dth_BrkNck_FOD_MALE_MALE_002
000000000000402e.ogg
Dth_BrkNck_FOD_MALE_MALE_003
0000000000004030.ogg
Dth_BrkNck_FOD_MALE_MALE_004
I'm kinda new to shell scripts, so any help will be appreciated. Thank you.
Seems like there is not direct connection b/w Original file name and New file name, To rename a file,
mv OriginalName NewName
one way is to repeat the step for multiple files, ( we can create all commands in a notepad and paste it altogether)
or we can put it in a shell script , say renamer.sh as below and execute it,
cat renamer.sh
mv 000000000000402a.ogg Dth_BrkNck_FOD_MALE_MALE_001
mv 000000000000402c.ogg Dth_BrkNck_FOD_MALE_MALE_002
mv 000000000000402e.ogg Dth_BrkNck_FOD_MALE_MALE_003
mv 0000000000004030.ogg Dth_BrkNck_FOD_MALE_MALE_004
to execute :- sh renamer.sh

Read a text file and use data in it as arguments in a command

I am writing a script to run in single user mode on a mac. I am having an issue because I am not able to figure out how to read data from a text file that contains this information
Guest
Shared
user
as three lines of unformatted text. I need to cd into these three directories which are subdirectories of /Users in order and run commands in each of the directories. The part Im having an issue with is only the reading of the text data line by line into cd /Users/$IneedToReadDataHer$/. If anyone knows how to do this it would be greatly appreciated.
cat file | while read line
do
cd /Users/"$line"/
# do something else
done

Shell Script to rename file with string from another filename

I have been stuck with a task where I need to rename a file with a substring from another filename and then move it to a directory.
What I want to achieve here is as below
I have a file named dummy.txt and another file named 20150416demo.xml
What I want is the file abc.txt to be renamed with the date from the xml file i.e. dummy20150416.txt
The final step is to move it to a different location. Appreciate you responses
Thanks
To rename/concatenate and move according to what you want you can use this script:
#!/bin/bash
file="20150416demo.xml"
date=${file:0:8}
mv abc.txt /destination/path/dummy$date.txt

Rename output of Shell script(s)

I am trying to create a script that runs an another script and changes the name from the output.
Here is the script so far:
#! /bin/bash
i=1
for N in mediainput.iso mediainput2.iso
do
x264transcode $N
mv $N $((i++))
done
This don`t that well. It just moves the files and renames them.
I need to first run the x264transcode and then rename the output of that. Since they all get the same name when x264transcode as processed the files.
Its okey that the name the files are changed to are 1 then 2 and so on.
But it would be a plus if there where a method of getting the name of the folder the file was inside or the file itself. Maybe choosing between them for different scenarios.
Example below:
~/Videos/Summer Vacation 2009/dvd.iso
Output from x264: VIDEO01.mkv
Output from rename script: Summer-Vacation-2009.mkv
Does x264transcode always call its output VIDEO01.mkv? Are all the video files dvd.iso? If so, something like this, to also get the correct filename with hyphens:
cd ~/Videos
for I in */dvd.iso
do
x264transcode $I
mv VIDEO01.mkv `dirname $I|tr ' ' -`.mkv
end
This is assuming x264transcode stores VIDEO01.mkv in the current directory rather than the directory its input file is located in.

How can I display a text file within a bash script?

Im trying to display a text file in a directory within the folder my script is in. I tried things like:
mypath=`realpath $0`
FILE="$realpath/Folder/Text.txt"
cat $FILE
And
FILE="$PWD/Folder/Text.txt"
cat $FILE
but they include the name of the file instead of just the its running from. I also want it to work with symbolic links.
cat "$(dirname -- "$0")/Folder/Text.txt"
That is, send the file Text.txt in the directory Folder below the directory where this script is located to standard output.
This will work with symlinks. And yes, all the quotes are significant.
You can do:
cat "./Folder/Text.txt"
the period denotes the current folder you are in. a double period "../Folder/Text.txt" denotes a folder up in the directory tree.

Resources