How to show ls path of the ls-piped-into-grep output? - linux

Does anyone know how to show the containing directory of an ls output command?
For example in the / I issue ls -R | grep something the output finds the file that matches the something without telling me where the file is located, or it's full path.

According to "ls" command,you can only search for files in the current directory, For global search,please use the "find" command.

Related

List all folder and subfolder inside it where folder names start with a* or b* or c* with path

I need a folder and subfolder inside it to be displayed where names that start with A* or B* or C* and display along with path
Below Command Does not Display as expected
$ ls -l | egrep d
You can display the current directory by using the system environment variable PWD. You can combine the PWD with your ls command
using ls -ld
ls -ld $PWD/A* $PWD/B* $PWD/C*
EDIT
If you want a list of all the directories and sub directories you can use the find command.
find . > subfolders.txt && cat subfolders.txt | egrep -i "^./E|^./g"
This command will recursively list all contents on your current working directory and send the output to a txt file named subfolders.txt. Then it will read the contents of subfolders.txt and using egrep, you can filter out anything that starts with "./E" or "./g". the -i option means it is case insensitive.
NOTE: This will also display the files contained in those subfolders.
find . | grep -E '/A|/B|/C'
find is better than ls for your requirements.

How to use ls command from module?

I want to run the commands like ls,cd,cat from my module.
Actually I want to run this command ls >> file.txt which makes a text file in that directory and save all the fle names in this text file...
How can I do this??
Not really sure what do you mean by from my module but assuming that from within your script you want to run ls command then you can do so like
`ls -l > input.txt`

How to append list of home directory to a text file and how to move files?

I know a very basic of unix and I am writing a java program which I only need couple of commands to solve my problem.
I appreciate if anyone knows how I can get desired output:
What command I can use to append a full list of objects of my home directory to a file "report.txt". Assume I am in different directory?
What command I can use to move all files in my directory that begin with either a, b, or c, to a subdirectory, sorting, of my current directory?
If I go to /proc directory. What does this command do?
cd 'ps | grep ps| cut -f 1'
I really appreciate if any one can help me out with these.
What command I can use to append a full list of objects of my home directory to a file "report.txt". Assume I am in different directory?
ls -la ~ > report.txt
What command I can use to move all files in my directory that begin with either a, b, or c, to a subdirectory, sorting, of my current directory?
mv a* b* c* yourdirectory
If I go to /proc directory. What does this command do?
cd 'ps | grep ps| cut -f 1'
This command will give "bash: cd: ps | grep ps| cut -f 1: No such file or directory" error

Partial directory list in linux

If I have a directory containing hundreds of files, using ls, ls-l, or dir gives me a list that's too long for the command terminal screen, so I'm unable to see most of the files in the directory.
I recall there being some argument for ls that allows one to scroll through the list in short increments, but can't seem to find it.
One option is to pipe the output to less or more
ls | less
or
ls | more
Try doing this in a shell :
ls -1 | less
One more way is to redirect the output of ls into a temporary file and then view that file with any editor of your choice - that way you can do searches etc. as well:
ls > res.tmp
vim res.tmp
emacs res.tmp
gedit res.tmp
grep "pattern" res.tmp

Comparing part of a filename from a text file to filenames from a directory (grep + awk)

This is not exactly the easiest one to explain in a title.
I have a file inputfile.txt that contains parts of filenames:
file1.abc
filed.def
fileq.lmn
This file is an input file that I need to use to find the full filenames of an actual directory. The ends of the filenames are different from case to case, but part of them is always the same.
I figured that I could grep text from the input file to the ls command in said directory (or the ls command to a simple text file), and then use awk to output my full desired result, but I'm having some trouble doing that.
file1.abc is read from the input file inputfile.txt
It's checked against the directory contents.
If the file exists, specific directories based on the filename are created.
(I'm also in a Busybox environment.. I don't have a lot at my disposal)
Something like this...
cat lscommandoutput.txt \
| awk -F: '{print("mkdir" system("grep $0"); inputfile.txt}' \
| /bin/sh
Thank you.
Edit: My apologies for not being clear on this.
The output should be the full filename of each line found in lscommandoutput.txt using the inputfile.txt to grep those specific lines.
If inputfile.txt contains:
file1.abc
filed.def
fileq.lmn
and lscommandoutput.txt contains:
file0.oba.ca-1.fil
file1.abc.de-1.fil
filed.def.com-2.fil
fileh.jkl.open-1.fil
fileq.lmn.he-2.fil
The extra lines that aren't contained in the inputfile.txt are ignored. The ones that are in the inputfile.txt have a directory created for them with the name that got grepped from lscommandoutput.txt.
/dir/dir2/file1.abc.de-1.fil/ <-- directory in which files can be placed in
/dir/dir2/filed.def.com-2.fil/
/dir/dir2/fileq.lmn.he-2.fil/
Hopefully that is a little bit clearer.
First, you win a useless use of cat award
Secondly, you've explained this really badly. If you can't describe the problem clearly in plain English it's not surprising you are having trouble turning it into a script or set of commands.
grep -f is a good way to get the directory names, but I don't understand what you want to do with them afterwards.
My problem now is using the outputted file with the one file I want to put the folders
Wut? What does "the one file I want to put the folders" mean? Where does the file come from? Is it the file named in inputlist.txt? Does it go in the directory that it matched?
If you just want to create the directories you can do:
fgrep -f ./inputfile.txt ./lscommandoutput.txt | xargs mkdir
N.B. you probably want fgrep so that the input strings aren't treated as regular expressions and regex metacharacters such as . are ignored.

Resources