Day to day linux commands
Posted on: 02-12-2012 by: mihai
Using ZIP / UNZIP to manage archives
1 | zip archivefile1 doc1 doc2 doc3 |
This command creates a file “archivefile1.zip” which contains a copy of the files doc1, doc2, and doc3, located in the current directory.
1 | zip archivefile1 * |
This command creates a file “archivefile1.zip” which contains a copy of all files in the current directory in compressed form. However, files whose name starts with a “.” are not included. The extension “.zip” is added by the program.
1 | zip -r archivefile1 . |
This copies the current directory, including all subdirectories into the archive file.
1 | zip -r archivefile2 papers |
This copies the directory “papers”, located in the current directory, into “archivefile2.zip”.
1 | unzip archivefile1.zip |
This writes the files extracted from “archivefile1.zip” to the current directory.
Using TAR to manage archives
1 | tar cvf alldocs.tar *.doc |
To create an archive using tar, use a command like this, which bundles all the files in the current directory that end with .doc into the alldocs.tar file
1 | tar cvf panda.tar panda/ |
Creates a tar file named panda.tar containing all the files from the panda directory (and any of its subdirectories)
In these examples, the c, v, and f flags mean create a new archive, be verbose (list files being archived), and write the archive to a file.
1 | tar cvzf alldocs.tar.gz *.doc |
To automatically compress the tar file as it is being created, add the z flag.
1 | tar xvf panda.tar |
Extract files from panda.tar. To extract the contents of a tar file, use the x (extract) flag in a command.
1 | tar xvfz panda.tar.gz |
Extract files from panda.tar.gz
c Create a new archive.
t List the contents of an archive.
x Extract the contents of an archive.
f The archive file name is given on the command line (required whenever the tar output is going to a file)
M The archive can span multiple floppies.
v Print verbose output (list file names as they are processed).
u Add files to the archive if they are newer than the copy in the tar file.
z Compress or decompress files automatically.
USERADD GROUPADD USERMOD
1 | useradd -G group-name username |
Add new user to existing group.
1 | usermod -a -G group-name username |
Add an existing user to an existing group.
1 | useradd -g group-name username |
Add a new user to primary group.
1 | usermod -g group-name username |
Change user’s primary group.
Listing files / folders
1 | ls -l | grep '^d' | awk '{ print $9 }' |
This command will display only the names of all directories within current directory (not recursive)
1 | ls -l | grep '^d' |
This command will display all directories within current directory (not recursive)
1 | find . -maxdepth 1 -type d -exec basename {} \; |
This command will display all directories within current directory (not recursive), but you will get the directory itself as well.
1 | find . -maxdepth 1 -mindepth 1 -type d -exec basename {} \; |
This command will display all directories within current directory (not recursive), without the directory itself.
1 | ls -F | grep / |
This command will display all directories within current directory (not recursive). For directories the -F directive adds a / after the dir name.
1 | ls -F | grep -v / |
To get only the files, you can grep by reversing the match (-v):
1 | ls -d -1 */ | tr -d '/' |
To get only the directories, without the / at the end. tr removes the ending / from the dir list
OR you can accomplish the same using this command:
1 | ls -d ./*/ | cut -d / -f 2 |
Categories: Linux




