Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Finding and renaming files with exceptions Post 302970111 by Don Cragun on Saturday 2nd of April 2016 02:59:04 AM
Old 04-02-2016
Quote:
Originally Posted by azurite
Hello all,

I am a new ubuntu user (have to use it for work) and I am trying to learn and familiarize myself with commands that I will be using frequently.

I would like some help in how I can get a list of all files with certain keywords in the filename.
Welcome to the UNIX & Linux Forums. We are here to help you.
Quote:
For example, I have a directory with numerous subdirectories that have a bunch of files in them. Two of those files contain the following format in the name: numericals_eddy_corrected.nii.gz and numericals_eddy_corrected_brain_mask.nii.gz.
I want to get a list of all the eddy_corrected.nii.gz and eddy_corrected_brain_mask.nii.gz files.
Please search the forum for similar questions that have already been answered before posting a new question. Without knowing what the format of other filenames in your directories, you could use a find command searching exactly for filenames ending with those two strings in the file hierarchy rooted in the current working directory:
Code:
find . -name '*eddy_corrected.nii.gz' -o -name '*eddy_corrected_brain_mask.nii.gz'

If there aren't any other names containing eddy_corrected that end with nii.gz you could use the simpler:
Code:
find . -name '*eddy_corrected*nii.gz'

Quote:
I am wondering, what command do I type in terminal so that I get a list, including the path, of all the files that have eddy_corrected in the name without the other eddy_corrected_brain_mask' showing up as well and vice versa? Is there a way to have terminal make a .txt file of the list?
The terminal application running in your window manager emulates a terminal from days of yore. The terminal application just emulates hardware, it doesn't run commands, it doesn't create file, it doesn't do anything except emulate a terminal device. When you start (or your system starts) the terminal application, you specify a program to run in that terminal window. Usually the program that you run in a terminal window will be a command interpreter such as bash, csh, ksh, or zsh. These command interpreters are known as shells. The commands recognized by different shells varies. All of the examples I'm suggesting in this post will work with bash, ksh, and zsh; some, but not all, will work in csh.
Adding > filename to the end of either of the above commands will redirect the output from those commands to the file named filename (creating the file if it does not exist, and overwriting its previous contents if it did exist). Using >> filename will append the output of those commands will create the file if it did not exist and append the output to the end of the file if it did exist.
Quote:
Second thing I need help with is renaming some files in those subdirectories. These files end in the extension .bvec and .bval and have absurdly long names with numbers in the format of subjectnumberDTISiementsTClessnumericals.bvec/.bval or 1000047785DTISiemensTCless005.bvec etc.
How can I rename all the files in the subdirectories to something simpler or shorter like subjectnumber_DTI.bvec?
You could try something like:
Code:
find . -name '*DTISiemensTCless*.bvec' -o -name '*DTISiemensTCless*.bval' |
while read -r path
do	dir="${path%/*}"
	file="${path##*/}"
	subjectnumber="${file%%DTI*}"
	ext="${file##*.}"
	echo mv "$path" "$dir/${subjectnumber}DTI.$ext"
done

(this assumes that you want to preserve the .bvec or .bval at the ends of the pathnames) and if the output looks like it correctly supplies the proper arguments to rename files the way you want them to be, remove the echo shown in red and rerun the command to actually rename the files. If you want to the new names of all of the files to have the extension .bvec, change the two lines:
Code:
	ext="${file##*.}"
	echo mv "$path" "$dir/${subjectnumber}DTI.$ext"

to be just:
Code:
	echo mv "$path" "$dir/${subjectnumber}DTI.bvec"

Quote:
I have heard of find and grep but I'm not sure how to go about combining commands to achieve what I need to do.
I will be required to do similar tasks in the future so I figured I should learn it once and for all. I would appreciate any help.

If I have posted this in the wrong forum, please feel free to move it to the correct location.
This is a perfectly reasonable forum for this topic.
Quote:
Thank you in advance.
Moderator's Comments:
Mod Comment Please use ICODE tags, not B tags for partial line sample input, output, and code segments.
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies

2. Shell Programming and Scripting

Renaming the files

Hello, i wanna rename my files which names are written in movies.txt films.txt = amovie bmovie cmovie dmovie emovie and i wanna find this files and rename the files to 1_amovie ... (12 Replies)
Discussion started by: redbeard_06
12 Replies

3. Shell Programming and Scripting

renaming files, please help

I want to rename the files by taking part of the file and appending date to it. please help e.g. abc-390.csv xyz-908.csv desired format is abc_YYYYMMDD.csv This is what I have but it is not working for each in *.csv; do mv $each /abc/data/"`date '+test_%Y%M%M'`".csv done (2 Replies)
Discussion started by: mqasim
2 Replies

4. Shell Programming and Scripting

Finding files older than the current date and time and renaming and moving

Hi, I have a very urgent requirement here. I have to find all files in the specified directory but not in the sub directories(The directory name is stored in a variable) which are older than the current date as well as current time and rename it as filename_yyyymmddhhmmss.ext and move it into a... (7 Replies)
Discussion started by: ragavhere
7 Replies

5. Shell Programming and Scripting

renaming files

Hello, I wanted to rename one file where filename contains space.. How can i rename in unix? The file name is ABC XYZ.TXT I wanted to rename this file as ABCXYZ.TXT. Any help is greatly appreciated... Regards. (4 Replies)
Discussion started by: govindts
4 Replies

6. Shell Programming and Scripting

renaming files or adding a name in the beginning of all files in a folder

Hi All I have a folder that contains hundreds of file with a names 3.msa 4.msa 21.msa 6.msa 345.msa 456.msa 98.msa ... ... ... I need rename each of this file by adding "core_" in the begiining of each file such as core_3.msa core_4.msa core_21.msa (4 Replies)
Discussion started by: Lucky Ali
4 Replies

7. Shell Programming and Scripting

Renaming files

Hi i have to achieve the following i have files as xyz001.csv, xyz002.csv.......xyz0025.csv in a folder, i need to keep xyz001.csv as it is but want to remove the extra zero on filename from 10 say xyz0010 should be renamed to xyz010 xyz0025 should be renamed as xyz025 Note xyz... (8 Replies)
Discussion started by: mad_man12
8 Replies

8. Shell Programming and Scripting

Renaming files

Hello, I am looking for a command line that will rename name files : f700_abc_o_t_MASTERID_AS_AE_20130323.csv like this f700_abc_o_t_MASTERID_AS_AE_20130324.csv The great idea could be to get the date stamp 20130323 and change any part of it, instead of just change the... (4 Replies)
Discussion started by: Aswex
4 Replies

9. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies
All times are GMT -4. The time now is 07:32 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy