Using one command line count the lines in the last file located in /etc in ubuntu - linux

ls /etc | tail -1 | wc -l
so basically I used this command but it counts the number of files that I've got from the tail command (which is the last file in the directory=1) but I didn't get the number of lines that are in the file.
and I used the cat command to open the file and count the lines but it didn't work.
ls /etc | cat tail -1 | wc -l
ls /etc | tail -1 | cat |wc -l

You could use xargs to use the result of the tail as an argument for wc, although I'd recommend using find instead of ls so you get the full path and don't need mess around with relative pathes:
$ find /etc -type f | tail -1 | xargs wc -l

You should never parse ls (instead parse /etc/*)
$ wc -l < `find /etc -maxdepth 1 -type f | tail -n 1`
or
$ find /etc -maxdepth 1 -type f | tail -n 1 | wc -l
What this does is find the last file for /etc
And puts it's content in wc -l

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

Find Most Recent File in a Directory That Matches Certain File Size

I need to find the most recently modified file in a directory that matches 3.0 MB.
First Attempt
ls -t /home/weather/some.cool*.file | head -n +1 | grep "3.0M"
Second Attempt
find /home/weather/ -maxdepth 1 -type f -name "some.cool*.file" -size 3M -exec ls -t "{}" +; | head -n +1
Am I close?
I hope this is of some use -
ls -ltr --block-size=MB | grep 3MB
The latest modified files will be displayed at the bottom of the output.
The -r flag shows the output in reverse order and the --block-size=MB will show the size of files in MB.
This should work:
ls -lh --sort=time /path/to/directory/*.file | grep "3.0M" | head -n =1

Return the number of directories

I have this piped a command that tells me how many directories are inside the current directory:
ls -lR | grep ^d | wc -l
But is there a way to check for a given directory? Something like:
ls -lR | grep ^d | wc -l /folder1/?
I think your just passing /folder1 to the wrong cmd
ls -lR /folder1 | grep ^d | wc -l
I suggest you use find. With -type d you can tell find to search only for directories. Like so
find /folder1 -type d | wc -l
The advantage is that you can easily change this to retrieve the names of the directories and also act on them with -exec.
The drawback is that this command also counts the directory /folder or ./, but that's easily circumvented:
find /folder1 -mindepth 1 -type d | wc -l

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

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