Need to iterate backwards over a linux path [duplicate] - linux

This question already has answers here:
Bash: search up a directory tree
(2 answers)
Closed 14 days ago.
0
I have a script that would have multiple paths (folder names).
Each folder path would have a Dockerfile, but its uncertain at what level that file would exist.
Lets say my path is "v1/airflow/1.86/src/code/"
Bu the file can be at Like for eg "v1/airflow/1.86/src/Dockerfile" or it can be at "v1/airflow/1.86/Dockerfile"
so i am trying to figure out a way where i can take a step back or cd ../ check recursively if the file exist there, if not then go one directory back, and look again and if it does, stop looking further
Any help is appreciated
I was looking a AWK for it, but nothing useful came out of it

You can do something like this
#!/bin/bash
while ! [ -f "File.txt" ]; do
echo "file not found!"
cd ..
done
echo "file found!"
At the end of the loop you will be in the directory with the file. You might want to add a counter that exits the while loop after X iterations so it doesn't become an infinite loop if the file doesn't exist.

Related

shell command to obtain files from different folders

I have question related to obtaining files from different folders using shell command
I have the following directory name
TC1-T1
TC1-T2
TC1-T3
TC1-T4
I want to obtain the files from 1st directory (TC1-T1) , 2nd directory (TC1-T2), and then the loop goes to TC1-T3 (3rd) and 4th directory as (TC1-T4). I wrote the following code but it did not work. I am looking forward if someone can help me in editing the shell command.
for pair in "TC"{1..2}-"T"{1..10}; do for i in $pair; do printf ${i}+1 printf "\n" done done

How to run a script for all files in Linux directory? [duplicate]

This question already has answers here:
How to loop over files in directory and change path and add suffix to filename
(6 answers)
Closed 4 years ago.
New to Linux here, sorry for the (easy?) question:
I have a script in Linux called script_run that works fine if I run it once and manually designate filenames. The script reads an input file, interpolates the data, and then saves the interpolated data to an output file. I'd like the output file to have the same name, except with a "_interp" added. I want to automate the script to run for all files in the file folder directory. How do I do this? My attempt is below, and I am running the script in the file folder directory, but it fails to loop. Thank you for your help!
FILEFOLDER=$*
for FILES in $FILEFOLDER
do
script_run "
[--input ${FILES}]
[WriterStd --output tmp_output_${FILES}.txt]"
cat tmp_output_${FILES}.txt >> output_${FILES}_interp.txt
done
#!/bin/bash
FILES=`ls *.*`
for FILE in $FILES
do
script_run "[--input ${FILE}] [WriterStd --output tmp_output_${FILE}.txt]"
cat tmp_output_${FILE}.txt >> output_${FILE}_interp.txt
done
btw what's with this strange annotation [--input ${FILE}] ? Does your script explicitly requires a format like that?

How do I change directory to home directory in Script? (bash) [duplicate]

This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 5 years ago.
Hi this is my first time writing bash and I'm trying to practice creating a bash script and am attempting to do the following:
Using absolute path the script changes directory to your home directory.
Next it verifies whether a subdirectory called project exists. If it does not exist it creates that directory.
Next it changes directory moving into the project subdirectory using relative path.
I'm already stuck on the first step:
#!/bin/bash
cd
if [ -d ./project ]
then
echo hi it exists!
else
echo it doesn't exist!
cd project/
I would appreciate if someone could help! thank you so much!
cd by itself changes the current working directory to the home directory.
See man cd for other uses of this command.
You can use the $HOME environment variable. Bash also recognizes a tilde (~) as home.

Bulk Folder Rename

I'm trying to get a shell script to go through a directory and rename the sub-directories (depth of 1). Basically, I'm looking for it to show the current directory name and prompt me for the name to change it to. To further specify, some of the folders will not need to be renamed, so I'm also looking for a way to skip a folder. I'm thinking something like an
#! /bin/bash
for dname in ./*/; do
echo $dname
echo What would you like the new name to be?
read newdname
mv "./$dname" "./$newdname"
done
But that doesn't include skipping folders. I'm thinking it needs an 'if' statement to allow me to enter 'skip' at the prompt to skip the folder.
What's the question? Do you want to know how to write the if statement? Probably like this:
if [[ $newdname != skip ]] ; then
mv "./$dname" "./$newdname"
fi
Or do you want to know whether it's a good idea? It probably isn't, as there might be a need to name a directory 'skip'. Using an empty string as a "skip" makes more sense, as you can't rename a directory to an emtpy name. In such situation, change the condition to if [[ $newdname ]].

RH Linux Bash Script help. Need to move files with specific words in the file

I have a RedHat linux box and I had written a script in the past to move files from one location to another with a specific text in the body of the file.
I typically only write scripts once a year so every year I forget more and more... That being said,
Last year I wrote this script and used it and it worked.
For some reason, I can not get it to work today and I know it's a simple issue and I shouldn't even be asking for help but for some reason I'm just not looking at it correctly today.
Here is the script.
ls -1 /var/text.old | while read file
do
grep -q "to.move" $file && mv $file /var/text.old/TBD
done
I'm listing all the files inside the /var/text.old directory.
I'm reading each file
then I'm grep'ing for "to.move" and holing the results
then I'm moving the resulting found files to the folder /var/text.old/TBD
I am an admin and I have rights to the above files and folders.
I can see the data in each file
I can mv them manually
I have use pwd to grab the correct spelling of the directory.
If anyone can just help me to see what the heck I'm missing here that would really make my day.
Thanks in advance.
UPDATE:
The files I need to move do not have Whitespaces.
The Error I'm getting is as follows:
grep: 9829563.msg: No such file or directory
NOTE: the file "982953.msg" is one of the files I need to move.
Also note: I'm getting this error for every file in the directory that I'm listing.
You didn't post any error, but I'm gonna take a guess and say that you have a filename with a space or special shell character.
Let's say you have 3 files, and ls -1 gives us:
hello
world
hey there
Now, while splits on the value of the special $IFS variable, which is set to <space><tab><newline> by default.
So instead of looping of 3 values like you expect (hello, world, and hey there), you loop over 4 values (hello, world, hey, and there).
To fix this, we can do 2 things:
Set IFS to only a newline:
IFS="
"
ls -1 /var/text.old | while read file
...
In general, I like setting IFS to a newline at the start of the script, since I consider this to be slightly "safer", but opinions on this probably vary.
But much better is to not parse the output of ls, and use for:
for file in /var/text.old/*`; do
This won't fork any external processes (piping to ls to while starts 2), and behaves "less surprising" in other ways. See here for some examples.
The second problem is that you're not quoting $file. You should always quote pathnames with double quoted: "$file" for the same reasons. If $file has a space (or a special shell character, such as *, the meaning of your command changes:
file=hey\ *
mv $file /var/text.old/TBD
Becomes:
mv hey * /var/text.old/TBD
Which is obviously very different from what you intended! What you intended was:
mv "hey *" /var/text.old/TBD

Resources