This question already has answers here:
Globbing certain YYYYMMDD dates
(2 answers)
Closed 3 years ago.
I'm trying to delete specific folders in a sub-directory. For example, I have a directory where output of a specific result from something else collects, like:
Jobs/Market/Java
In that directory, I'll have outputs like these with dates and some random numbers in the end:
OUTPUT_201908809_134243
OUTPUT_201908810_242323
OUTPUT_201908811_152342
OUTPUT_201908815_124243
I'm trying to run a job that deletes folders that have '201908809'-'201908811' and leaves the remaining ones.
I've tried to manually deleting them using rm -r and the folder name. I know there has to be an easier way. I've looked up some things on stack and found lines that only delete folders that have an starting or and ending of the folder name.
What's the standard way of accomplishing this with Bash?
You can use wildcards for rm for example in this case you can use rm -rf *_201908809_* * means match all characters
https://www.shell-tips.com/2006/11/04/using-bash-wildcards/
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I work in a software development company and every day upon my machine boot I have to execute the same commands to start coding. I recently decided to create a bash script to do that for me. The problem is that the commands I need to type have a single difference from one another, and that is the folder I need to access.
I always have to access a directory where it will contain folders with different versions of the company code (let's call it "codes" for the sake of the discussion), and everyday another folder is added to the "codes" directory (they update the company code everyday) with name as timestamp e.g. 2021-07-05-17-52-51.
To be able to create my automation script I need to be able to get into the "codes" directory and get the most recent folder added to it, or get the latest timestamp.
I am new to bash and I couldn't find answers on how to get the last added folder to a directory using bash or someway to use tab and get the last one.
You can use something like this:
directory=$(ls -At1 | head -n 1)
An explanation in parts:
ls -At1 lists sorted by time with one entry per line
head -n 1 returns the first entry
$(...) runs the command as a subshell, evaluates, and sets directory to the name of the item with the most recent modified datestamp. If you want to ignore hidden files and folders, you can lose the -A flag from ls.
This question already has answers here:
Get current directory or folder name (without the full path)
(24 answers)
Closed 7 years ago.
So long story short I have to make a Shell Script where I need to have something like two variables, the first one contains a path read from the keyboard (something like:this/is/the/path/I/need)
I really need to extract the last folder of that path example, and put it in another variable, in my example I need to get out of the path the "need" part and put it in the second variable. How can I do this? The fact that this is read from the keyboard makes it pretty hard to do in my opinion. Thanks!
$ read path
this/is/the/path/I/need
$ directory=$(basename $path)
$ echo $directory
need
$
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have several directories structured like this in a parent directory:
/app/bpp/cpp/dpp/ASM/Report
/ghh/hhh/hhh/ASM/Report
/hh/ASM/Report
As we see above, all the ASM directories have Report directories in them along with other sub directories and files. I want a separate directory that has a parent directory to ASM (with ASM only), and ASM with Report directory in it. The result should look like this:
/dpp/ASM/Report
/hhh/ASM/Report
/hh/ASM/Report
It's not absolutely clear what you are asking; do you want to make a copy of the initial directories; do you want rather to move the initial directory to a new location? (Since you seem to want something related to "shell script" you should also tag your question with these words).
The best would probably to start with find; the following command:
find / -type d -name Report
will list all directories called Report; you could pipe the output of this command to grep in order to select those ending with /ASM/Report with:
find / -type d -name Report | grep "\/ASM\/Report$"
this would give to you a good starting point for detecting the directories to be moved/copied.
You can also use the -exec option of find for directly perform some action on a file or directory found by the command. You should type man find in order to see all the power of this tool.
It looks like you will have to search in the whole filesystem; thus find may print some warnings (related to permissions), but it shouldn't hurt; you can discard these warnings (if any) by ending the find command with 2>/dev/null for discarding the stderr stream (the error messages).
This question already has answers here:
How do I remove the file suffix and path portion from a path string in Bash?
(15 answers)
Closed 7 years ago.
Here's where I'm stuck up.
I've a old file (Fieldart_1.2.war). And I've a new file named Fieldart_1.4.war.
I just have to deploy the new one. But, before that I need to make sure I'm hitting the right file.
Is there anyway I can compare just the first part of the two files i.e., 'Fieldart' ? like, if the first part matches, go ahead with the deployment otherwise, throw an error..
What do you guys think ?
use the command find . -name "Fieldart*"
This question already has answers here:
Renaming multiples files with a bash loop
(3 answers)
Closed 8 years ago.
I have a directory, let's say its name is direct in which has multiple files whose names are .xyz, .abc, .klm and .etk4 etc. Is there a way to make all those hidden files visible at the same time instead of one by one? I know this question has been asked before here but I did not get the answer. I hope somebody can explain it to me in a simple way since I am not much familiar with linux.
for file in .[^.]*
do
mv "${file}" "${file#.}"
done
${var#prefix} expands to the value of $var with the initial prefix removed.