Problems with ls -l | grep combination - linux

i want to see all files that end with .sh through a ls -l | grep combination. The problem with that is that it has to show only the filename, no other attributes. How do I do this with a ls-l | grep combination?

You can solve this in two ways, one with grep and one without grep:
ls -a | grep "\.sh"
or
ls *.sh

Related

Counting files in a huge directory [duplicate]

This question already has answers here:
What is the best way to count "find" results?
(6 answers)
Closed 3 years ago.
Related to this question.
How do I count the number of files in a directory so huge that ls returns too many characters for the command line to handle?
$ ls 150_sims/combined/ | wc -l
bash: /bin/ls: Argument list too long
Try this:
$ find 150_sims/combined/ -maxdepth 1 -type f | wc -l
If you're sure there are no directories inside your directory, you can reduce the command to just:
$ find 150_sims/combined/ | wc -l
If there are no newlines in file names, a simple ls -A | wc -l tells you how many files there are in the directory. Please note that if you have an alias for ls, this may trigger a call to stat (Example: ls --color or ls -F need to know the file type, which requires a call to stat), so from the command line, call command ls -A | wc -l or \ls -A | wc -l to avoid an alias.
ls -A 150_sims/combined | wc -l
If you are interested in counting both the files and directories you can try something like this:
\ls -afq 150_sims/combined | wc -l
This includes . and .., so you need subtract 2 from the count:
echo $(\ls -afq 150_sims/combined | wc -l) - 2 | bc

showing list of directories using ls

I want to read all home directories of users on a Linux system.
I know I can use:
cut -d':' -f6 /etc/passwd | sort -u
to get a list of those directories.
How can I list each of those directories in detailed format, as when using ls -l?
For example, I would expect to get the root directory as:
dr-xr-x---. 4 root root 4096 Mar 2 02:49 root
Is there any option to pipe the list from the cut command to ls for showing the content?
I think this is what you're looking for:
cut -d':' -f6 /etc/passwd | sort -u | xargs -i% ls -ld %
You can use the following command line:
cut -d':' -f6 /etc/passwd | sort -u | xargs ls -ld

How does the word count program count hidden files?

After creating a few directories and hidden files and running the following commands
ls -al | wc -l
ls -a1 | wc -l
I get a difference in the total returned by the word count program. The
ls -al | wc -l
command returns one more count. Why is this?
$ ls -al | head -n 1
total 57600
This line is not shown with -1.
| is the pipe that connect the command, the output of left command ls -al is the input of right command wc -l.
output of command ls -al is string then wc -l will count the string as a file content,The file names in the string content isn't the argument for command wc -l.
the command xargs is useful,you can use it.
like:
ls -a | xargs wc -l
# find command to find files
find ./* | xargs wc -l

open 100 files in vim

I need to grep to tons (10k+) of files for specific words.
now that returns a list of files that i also need to grep for another word.
i found on that grep can do this so i use:
grep -rl word1 *
which returns the list of files i want to check.
now from these files (100+), i need to grep another word. so i have to do another grep
vim `grep word2 `grep -rl word1 *``
but that hangs, and it does not do anything,
why?
Because you have a double `, you need to use the $()
vi `grep -l 'word2' $(grep -rl 'word1' *)`
Or you can use nested $(...) (like goblar mentioned)
vi $(grep -l 'word2' $(grep -rl 'word1' *))
grep -rl 'word1' | xargs grep -l 'word2' | xargs vi
is another option.

Shell copying file with biggest size to another folder

I am new to Linux shell and I found a way to get the name of the file I want:
ls *.*g -S| grep -v ^d | head -1
I am going to be repeating this for a number of file. I am trying to copy this file to another directory (cp command?). But the below code is failing.
I am trying this, but its not working:
cp ls -S| grep -v ^d | head -1 ../directory
Also, I was wondering how to loop through directorys that are in a particular directory.
Any help is much appreciated.
Thanks,
Bryan
cp $(ls *.*g -S| grep -v ^d | head -1) ../directory
cp "$(find . -type f -name "*.*" -printf "%f:%s\n" | sort -t":" -k2 -n | awk -F":" 'END{print $1}')" ../directory
Use quotes in case there are white spaces in your file. add -maxdepth 1 if you don't want to recurse subdirectories

Resources