How to show the amount of files in the sub-folders? - linux

I have a folder named "A", and there are 3 sub-folders in it which are named "A1", "A2", "A3" respectively.
Is there a command to show the amount of files in each sub-folders?
E.g.
A1 5
A2 7
A3 18
Thanks.

Well, you can loop over each subdirectory in ./A and output the number of files/folders contained in each subdirectory with:
for i in A/*; do [ -d "$i" ] && echo "${i##*/} $(ls -1 "$i" | wc -l)"; done
Which just loops over all files and folders in A, and if the current name is a directory, it echos the directory name and a count of the number of files/folders in that directory.
(note: that is a ls -"one" and a wc -"ell")
To include hidden files in the subdirectories use ls -a1 which is -"a"one" and then subtract 2 from each total (for . and ..)
Give that a try and let me know if you have further questions.

Try this
ls -R /path/to/A-diretory
du -ah /path/to/A-direcory //here it will tell the size of files too
if you want to see your listing in tree like structure, use tree command, its a very powerful command having a lot of options.
before that
apt-get install tree (for ubuntu)
tree /path/to/A-directory
You can always refer man command to explore more of a command

Related

How can I list the files in a directory that have zero size/length in the Linux terminal?

I am new to using the Linux terminal, so I'm just starting to learn about the commands I can use. I have figured out how to list the files in a directory using the Linux terminal, and how to list them according to file size. I was wondering if there's a way to list only the files of a specific file size. Right now, I'm trying to list files with zero size, like those that you might create using the touch command. I looked through the flags I could use when I use ls, but I couldn't find exactly what I was looking for. Here's what I have right now:
ls -lsh /mydirectory
The "mydirectory" part is just a placeholder. Is there anything I can add that will only list files that have zero size?
There's a few ways you can go about this; if you want to stick with ls -l you could use e.g. awk in a pipeline to do the filtering.
ls -lsh /mydirectory | awk '$5 == 0'
Here, $5 is the fifth field in ls's output, the size.
Another approach would be to use a different tool, find.
find /mydirectory -maxdepth 1 -size 0 -ls
This will also list hidden files, analogous to an ls -la.
The -maxdepth 1 is there so it doesn't traverse the directory tree if you have nested directories.
A simple script can do this.
for file_name in *
do
if [[ !( -s $file_name ) ]]
then
echo $file_name
fi
done
explanation:
for is a loop. * gives list of all files in a current directory.
-s file_name becomes true if the file has size greater than 0.
! to negate that

How to copy over all files from one directory to another excluding ones that start with a given string in a bash script

I have a directory containing a large amount of files ~1gb. I need to copy over all of them except ones that start with "name" to a different directory. I tried using this: "ls src_folder | grep -v '^name' | xargs cp -t dest_folder" from this pervious question In Linux, how to copy all the files not starting with a given string?
I get the following error when trying to copy over test1.txt from src_folder which contains test1.txt and name.txt to dest_folder
cp: cannot stat `test1.txt': No such file or directory
My current work around is to copy over all of the files, then use find to delete the ones starting with "name" in the dest_folder. This works, but I imagine I could save some time by only copying over the files I really want. Any suggestions?
You can use the shell option extglob. This option extends the bash's pattern matching, so you can use more advanced expressions.
shopt -s extglob
cp src_folder/!(name*) dest_folder
For more info run nam bash and look for extglob.

Finding the oldest folder in a directory in linux even when files inside are modified

I have two folders A and B, inside that there are two files each.
which are created in the below order
mkdir A
cd A
touch a_1
touch a_2
cd ..
mkdir B
cd B
touch b_1
touch b_2
cd ..
From the above i need to find which folder was created first(not modified).
ls -c <path_to_root_before_A_and_B> | tail -1
Now this outputs as "A" (no issues here).
Now i delete the file a_1 inside the Directory A.
Now i again execute the command
ls -c <path_to_root_before_A_and_B> | tail -1
This time it shows "B".
But the directory A contains the file a_2, but the ls command shows as "B". how to overcome this
How To Get File Creation Date Time In Bash-Debian
You'll want to read the link above for that, files and directories would save the same modification time types, which means directories do not save their creation date. Methods like the ls -i one mentioned earlier may work sometimes, but when I ran it just now it got really old files mixed up with really new files, so I don't think it works exactly how you think it might.
Instead try touching a file immediately after creating a directory, save it as something like .DIRBIRTH and make it hidden. Then when trying to find the order the directories were made, just grep for which .DIRBIRTH has the oldest modification date.
Assuming that all the stars align (You're using a version of GNU stat(1) that supports the file birth time formats, you're using a filesystem that records them, and a linux kernel version new enough to support the statx(2) syscall, this script should print out all immediate subdirectories of the directory passed as its argument sorted by creation time:
#!/bin/sh
rootdir=$1
find "$rootdir" -maxdepth 1 -type d -exec stat -c "%W %n" {} + | tail -n +2 \
| sort -k1,1n | cut --complement -d' ' -f1

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 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

Resources