How can i show the correct answer? - linux

I'm working on a school stuff and the question is to :
Use the ls command to list the files in the directory 'apache2/mods-available'.
List only files beginning with 'a' and have the file extension '.conf'.
List one file per line in directory.
Enter your bash code within $ () to execute it and return the response for exemple : ANSWER=$( find . -name 'filename' )
I tried to resolve the problem like this :
Answer = $(ls -1 apache2/mods-available/a*.conf)
And than i get this answer :
It's unfortunately wrong because the rigth answer is :
Do someone has an idea about what's wrong? thanks :)

You can use the follow script
Answer=$(find apache2/mods-available/ -maxdepth 1 -name 'a*.conf' -exec basename {} \;)
maxdepth is to search in the directory 1 depth
-exec basename {} \; is to get the name

Try it with Answer=$(ls -1 a*.conf)


Linux find catalogs and print only names

i wanna search for the catalogs which have the "program" in their names and echo these names in console. I have wrote this, but isn't working:
find usr -type d -name "program" -exec echo {}
The error is find: missing argument to `-exec'.
find usr -type d -name "program"
How to fix my command?
Some tiny example with the * wildcard.
find /my/path -name "*program*"
If you don' use wildcards, it will try to find exactly the files named program. Also, echoing is done automatically, you don't need the exec command.
Answering to your comment. You can get the base name (name without the path) with:
find . -name "*program*" -exec basename {} \;

Regarding searching a keyword in all files in particular directory in linux

I want to search a word suppose "abcd" in all the files(Including hidden and all possible files) in dir suppose /home/john/?
This is what I tried, I am running the below command and its getting stuck for more than 24 hours.
command --> find /home/john -type f -exec grep -iH 'abcd' {} \;
Result something which will show all the files which have this particular word or any file which is have the name as our search word.
What about using grep recursion option ?
grep -r abcd /home/john

how to execute a file from some deep directory? [closed]

Guys i want to execute a file, knowing only its filename but i dont know its exact location inside my home directory so i think im gonna use find to find it but i dont know the next step in executing it. And my file is in type perl
find /dir -name "filename" -exec {} \;
Would also be good to check that the file is executable...
find /dir -name "filename" -perm /u+x -exec {} \;
Try this
find /your/home/dir -name "" | xargs perl
This will locate the file then execute it. Of course, you could simply run find then type
perl /path/to/file/found/using/find/
If you don't want to type the full path you could copy-paste the fild result.

linux search file based on file name pattern

I want to search a series of files based on the files name.
Here is my directory :
For example I had the file on above.
I only want to search out the file which is without _bak.
If you're wanting to limit your search to the current directory, use:
find . -maxdepth 1 -mindepth 1 ! -name '*_bak'
If you want to recursively find in all directories remove the -maxdepth 1.
Edit in response to OP's comment
To get files that begin with ei and do not end with _bak use:
find . -type f -name 'ei*' -a ! -name '*_bak'
Please try this :
find . -type f ! -iname "*_bak"
The above command will find all files that have not end with _bak.
Use the Grep Invert Match option.
For example, if you want all the file without the word _bak, use :
grep -v *_bak /path/to/file

How to change file and directory names with find?

I changed project name and now I have many files an directories with old name. How to replace these names with find?
find . -name "*old_name*" -exec ???
This find should work for you:
find . -name "old_name" -execdir mv "{}" new_name +
This will find files with the name old_name from the current dir in all sub directories and rename them to new_name.
Below is what I have used in the past. The biggest gotcha is the RHEL rename (c) vs Debian rename (perl) - They take different options. The example below uses RHEL c based rename command. Remove the '-type f' to also rename the directories.
find . -type f -name "*old_name*" -print0 | xargs -0 -I {} /usr/bin/rename "old_name" "new_name" {}
newname=""; for files in $(find Doc2/scripts/ -name; do echo $files; dir=${files%/*}; cfile=${files##*/}; echo "$dir -- $cfile"; echo "mv $cfile $newname"; done
Doc2/scripts/gateway --
you could also add:
find . -maxdepth 1 -iname file
where maxdepth will ensure you dont need to worry about sub folders and iname means case sensitive
Ok, my solution:
find . -name "*old_name*" -exec rename 's/old_name/new_name/g' {} \;
But this works for directories which name not contain "old_name", otherwise find say for example:
find: `./old_name': No such file or directory
Because it trying search in "old_name" directory, and the directory is already a "new_name"
1. First, backup your directories and files
The following Bash code run on my OS X and Ubuntu boxes.
2. Rename the directories from old_dir to new_dir:
for d in $(find . -maxdepath X -type d -name 'old_dir'); do mv $d "$(dirname $d)/new_dir"; done
X is a number used to specify the depth of replacing old_dir
3. Rename the files from old_file to new_file:
for f in $(find . -type f -name 'old_file'); do mv $f "$(dirname $f)/new_file"; done
Don't care about #Benjamin W. and #ghoti them one for his ForMatted code and #ghoti try to orient the question to his Pitfalls.
Hi, #Benjamin W. what about this new post? and #ghoti did you run the above code incorrectly on your machine? If the code can't work just let me know or post a question pls, and if you had a better one pls post here let we know.