1. Write a function that takes as input a directory. The function will search through that directory and its full subdirectory tree, building a count of the files by their extensions. As in the lecture, use a dictionary to keep track of the counts. The return-value of this function will be a dictionary of (key,value) pairs where the key will be the file extension and the value will be the number of files with that extension. For example, for the file "script.py", the extension is ".py". The return value of your function will be a dictionary of the form { ".py" : 50, ".exe" : 15, etc } provided there are 50 python scripts and 15 exe files in the subdirectory tree. If a file has no extension (eg: the file named "README") store the extension as "". We consider the extension to be the information after the rightmost period in the file name. Apply your script to the directory "/". It might fail due to a variety of issues. How can you fix your script to ensure it does not fail? a) What is the most common file extension on the computer you are working on? b) What is the most common file extension in your GitLab repository? Build a stand-alone python script from your procedure that can be executed with a command "python typescript.py dirname".
2. Build another such Python function. This time, count files by their owner. i.e. this function will take as input a directory, and return a dictionary of pairs ("owner name" : number of files owned by this owner).
There is a two-step process to access user IDs, and there are two relevant libraries:
1) From a file you can access the user ID number or via os.stat.
2) The pwd library has the function getpwuid which gives access to the user ID as a string, from the user ID number.
Once you have setup this function, you should be able to answer the two questions:
a) Who is the most common file owner on the system you are working on?
b) Who is the most common file owner on your GitLab account?
3. Build another such Python function, but count files by the group they belong to.
1) From a file you can acces the group ID number via os.stat, much the same as in Exercise (2).
2) From the group ID number you can recover the group name (a string) via the grp library getgrgid call.
a) Among all the files on the server you are working on, which is the most common group membership?
b) In your personal GitLab repo, what is the most common group?
Step 1: Create a new python file called text.py and paste the following code.
<==================(this line is not in the code)==================>
import pwd
import os
from os import stat
from pwd import getpwuid
from grp import getgrgid
ft = dict()
def GetAllFiles(mypath):
files = os.listdir(mypath)
for f in files:
x = f.split(".")
if(len(x) >= 2):
x =
x[len(x)-1]
if(x in
ft):
ft[x] += 1
else:
ft[x] = 1
else:
GetAllFiles(mypath +"/" + x[0])
return ft
print("using the extension of files ")
print(GetAllFiles("/home/stark/Desktop/ds"))
def findOwner(filename):
return getpwuid(stat(filename).st_uid).pw_name
fo = dict()
def GetFileOwner(filepath):
files = os.listdir(filepath)
for f in files:
x = f.split(".")
if(len(x) >= 2):
o =
findOwner(filepath + "/" + f)
if( o in
fo):
fo[o] += 1
else:
fo[o] = 1
else:
GetFileOwner(filepath + "/" + x[0])
return fo
print("using the User name")
print(GetFileOwner("/home/stark/Desktop/ds"))
def findGroup(filename):
return getgrgid(stat(filename).st_gid).gr_name
fg = dict()
def GetGroupOwner(filepath):
files = os.listdir(filepath)
for f in files:
x = f.split(".")
if(len(x) >= 2):
o =
findGroup(filepath + "/" + f)
if( o in
fg):
fg[o] += 1
else:
fg[o] = 1
else:
GetGroupOwner(filepath + "/" + x[0])
return fg
print("using the Group owner")
print(GetGroupOwner("/home/stark/Desktop/ds"))
<=====================>
Step 2: change the directory according to yours and run your code.
Any problem ask me in the comment section.
Thumbs up is appreciated.
1. Write a function that takes as input a directory. The function will search through that...
Write a program to count a number of files for each extension in the given directory. The program should take a directory name as argument and print count and extension for each available file extension. For example, the output would be something like this: python counter.py \yourpathhere 14 py 4 txt 1 csv
Write a bash shell script, deleteFilesWithZeroLength.sh, that removes all zero length ordinary files in the directory passed as an optional argument. If you do not specify the directory argument, the script uses the present working directory as the default argument. Do appropriate exception handling in your script such as:If the arguments are more than 1, print out “Too many arguments passed”.If the argument passed is a regular file, print out “XXX is regular file”.1c. If the directory doesn’t exist, print out “Directory...
Python Problem Write a .py file like nameFiles.py. In that .py, create a function that strips the first letters from all file names in a directory. If the new name is a duplicate of an existing name, Python will throw an exception. Handle that exception by adding “dup” a number as part of the files new name. Call that function from a Jupyter Notebook, passing in the number of characters to delete and the file path.
Can you write a Python 2.7 function that takes a directory path, and prints the size, in bytes, of all the files, ignoring sub-directories, reverse sorted by size, with the total in the following exact format: file_name_2: 200000 file_name_1: 100000 file_name_3: 1000 > 3 File(s) - Total: 301000. Provide the copyable code.
Step 3: How would you write this script in C? It needs to do the things required in step 3. 1. You have received a new batch of distinguished users; their basic information is located in newusers.tar. Inside of the tar file, there is a file called "newusers.txt" which contains a colon-separated entry for each user: the username, the uid, the GECOS information, and the user's preferred shell. Also in the tar file you will find a public key for...
Homework No.2 CSC 222 Write a shell script program called "countf.sh" that will count how many files or directories recursively. beside the file counts, also report how many files for each types (directory or regular files). It will either take arguments for specific directories and no argument. If no argument, it will read the current working directory as the starting point. Please also provide -h option to print out how to use your countf.sh program % ./countf.sh % ./countf.sh file1...
Problem 4: Write a Bash script that removes all zero length ordinary files in the directory (including those in the sub-directories at all levels) passed as an optional argument. If you do not specify the directory argument, the script uses the current working directory as the default argument. This problem is for practicing bash programming skills. Though there is an easier way to achieve the goal with the find command, the find command is not allowed to appear in your...
Cybersecurity Fundamentals Lab #9: Access ControlName: ____________________ If your Kali VM is already up and running, login to the root account on the VM via ssh. It does not matter whether you use a command line or GUI sshclient. If your VM is not currently up, use the vSphere web client to start your VM before logging into it via ssh.1: Create an Unprivileged User For some labs, including this one, we will need a non-root user account on the...
In this assignment, you will demonstrate your ability to write simple shell scripts. This is a cumulative assignment that will challenge you to pull together a variety of lessons from throughout the course. 2 The Assignment Every now and then, I find myself with a large number of files that have inappropriate extensions (the set of characters in the file name after the last :') that need to be changed. For example, a complicated C++ program, developed by someone on...
Write a bash script to find all the files ending with .c recursively for every directory in your current working directory, then copy each one to a folder called programs, need handle duplicates by appending the number to the end of the file (ex main.c main-1.c ) compile them and generate a report report should look like: main.c compiles prog2.c failed main-2.c compiles etc.