linux : compare just a part of two directory names [duplicate] - linux

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*"

Related

How to get the last added folder to a directory [closed]

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.

Deleting specific files ending with specific strings in Linux [duplicate]

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/

Efficiently editing content near the top of a very large file on Linux [duplicate]

This question already has answers here:
Working with huge files in VIM
(10 answers)
How to edit multi-gigabyte text files? Vim doesn't work =( [closed]
(15 answers)
Closed 6 years ago.
I have a db dump file that is over 5 gigs in size and I'm looking to do a quick edit to the create database and use database command. This dump is provided to me.
I've been using vim to do this from the command line, but it takes a while to load. I'm able to use less to read very quickly. Is there a way to edit the file without having to wait several minutes for the full file to load in vim? This can be a param passed to vim, or different common way to edit files from command line.
I'm looking for a general solution that I can apply to other large files too, so would like a linux command that would allow me to edit the top of the file quickly.
You can use cat:
cat file_with_create_cmd db_dump > new_dump
If you want to use that in a subsequent command instead of writing it to a file, you may use process substitution:
process_dump <(cat file_with_create_cmd db_dump)

Linux Shell Script cut last part of variable which contains a path [duplicate]

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
$

converting hidden files in to normal files in linux [duplicate]

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.

Resources