IT/Software/Command Line: Difference between revisions
Walttheboss (talk | contribs) |
Walttheboss (talk | contribs) No edit summary |
||
(40 intermediate revisions by the same user not shown) | |||
Line 6: | Line 6: | ||
==Basic Commands== | ==Basic Commands== | ||
===Find=== | |||
This will help you find files and/or folders. Its power is amazing. | |||
It is often used in conjunction with commands. First find something and then do something. | |||
<code>find directory -user username</code> | |||
Directory is relative unless you specify absolute | |||
Example: This will find all files and directories in the current directory AND recursively. Then it will touch the file changing its mtime and for most computers that is the timestamp that is read. Nextcloud will not accept old timestamps and so we have to use this command to clean things up. Lots of great "find" stuff [https://www.systemconf.com/2021/01/14/linux-file-creation-touch-command/ here]. | |||
<code>find . -exec touch {} \;</code> | |||
for some more security you can | |||
<code>find . -execdir touch '{}' \;</code> | |||
-execdir runs each command from the directory in which the find result is located. [the curly brace pair] should be quoted (for example, '{}') to protect it from interpretation by shells". | |||
===Changing Ownership=== | ===Changing Ownership=== | ||
Line 20: | Line 39: | ||
This changes only the directory to user and group specified | This changes only the directory to user and group specified | ||
Adding the -R flag after the chown will | Adding the -R flag after the chown will recurse into directories and files. | ||
<code>sudo chown -R . . . </code> | |||
*We are not sure why the capital r is needed in some cases. But it is here. | |||
===Changing Permissions=== | ===Changing Permissions=== | ||
This is very similar except we use chmod instead of chown | This is very similar except we use chmod instead of chown | ||
<code>sudo chmod 644 /path/to/file or path/to/directory/ </code> | |||
Adding the -R flag does the same as above. | |||
====Change all files recursively to 644==== | |||
<code>find /var/www/html/moodlecnx/ -type f -exec chmod 644 {} \; </code> | |||
====Change all directories recursively to 755==== | |||
<code>find /var/www/html/moodlecnx/ -type d -exec chmod 755 {} \; </code><br /> | |||
you may need a "sudo" in front of chmod depending on where you are working. | |||
<br /> | |||
===Make a text file of the filenames in a directory=== | |||
<code>find -maxdepth 1 -type f ! -name flist.txt -printf "%P\n" > flist.txt</code> | |||
Command information: | |||
*<code>-maxdepth</code>: Don't search for files below folders of one level down. | |||
*<code>type f</code>: Search for only files | |||
*<code>-printf "%P\n"</code>: Print the names only and on separate lines | |||
*<code>> flist.txt</code>: Store those names (using output redirection) in a file to be created on the fly called `flist.txt | |||
*<code>! -name flist.txt</code>: Skips the name of the output file from the generated list | |||
===Make Directory=== | |||
<code>mkdir nameOfFolder</code> | |||
<code>sudo mkdir name1 name2 nameX</code> | |||
<code>mkdir -p /Path/newFolder</code> | |||
===Mount and Unmount=== | |||
====iso files==== | |||
<code>sudo mkdir /mnt/iso </code> | |||
<code>sudo mount -o loop Win10_2004_English_x64.iso /mnt/iso </code><br /> | |||
===Symlinks=== | |||
This allows you to click on a folder or directory and actually end up somewhere else. | |||
For example if a user can't figure out how to get to the Documents folder from Dolphin.<br /><code>ln -s /real/file/or/folder/path /path/to/link</code> | |||
The link can't already exist. | |||
the path can be a file or directory. | |||
===Delete all files in folder tree=== | |||
This will find all files under the parent directory(absolute) and recursively delete them. | |||
It will not delete hidden files. | |||
<code>find ~/msgcnxFiles/Teacher/Science/Biology/For\ Upload/ -type f -delete</code> | |||
===zip Multiple Files with Date=== | |||
<code>zip -r path/from/here/"trial-$(date +"%Y-%m-%d")" Folder1 Folder2 Folder3</code> | |||
-r recurse into directories | |||
You are adding the date command into the filename. | |||
Then list all the wanted folders. | |||
Note: The command is relative. It knows the folder you are in. You can specify all paths relative to the current folder or start with a / and you now have absolute referencing. | |||
===Delete files older than a specified time=== | |||
<code>find /home/walt/.config/Nextcloud/logs -name '*.gz' -type f -mmin +240 -exec rm {} \;</code> | |||
The above command will delete all files older than 240 minutes | |||
You can change to -mtime +7 for older than 7 days. | |||
===Printing=== | |||
lp is the classic | |||
lp filename will print the file to the default printer with the default settings | |||
<code>lp -n 3 /path/*.pdf</code> | |||
Much time has been invested in learning this. | |||
Make sure you install the printer correctly or you will get strange errors. | |||
Stay away from "driverless" if you can. | |||
List the default destination | |||
<code>lpstat -d </code> | |||
List the available printers | |||
<code>lpstat -d -p</code> | |||
List all jobs | |||
<code>lpstat -o</code> | |||
Cancel all jobs and delete them. | |||
<code>cancel -a -x</code> | |||
Print Portrait two sided. | |||
<code>lp -o sides=two-sided-long-edge </code> <br /> | |||
3 copies of any pdf file in that path. | |||
-P page-list Specifies pages to print in the document. The list can contain a list of numbers and ranges (#-#) separated by commas, e.g., | |||
"1,3-5,16". The page numbers refer to the output pages and not the document's original pages | |||
<br /> | |||
Print a directory | |||
<code>find -name "*Test.*" -exec lp -o sides=two-sided-long-edge {} \; </code> | |||
===Rename=== | |||
*This function has many many options. We can only begin here. | |||
*Below is an example where we prepend a 0 onto file names so that they order correctly on all OSes | |||
*This works in the current working directory | |||
<code>rename -e 's/\d+/sprintf("%02d",$&)/e' -- *.jpg</code> | |||
<br /> | <br /> | ||
===Kill=== | |||
*This function has many many options. We can only begin here. | |||
Find the process id (pid) you want to kill and kill it. | |||
<code>kill -9 3567</code> | |||
If you know the name you can kill by name. | |||
<code>killall -9 vlc</code> | |||
<br /> | |||
==More modifiers== | ==More modifiers== | ||
__FORCETOC__ | __FORCETOC__ |
Latest revision as of 07:29, 9 April 2024
Welcome to the dark world where most IT people live
- This is a gui free world
- If you don't know what a gui is please quietly close the page and go buy a slurpee
- If you don't know what a slurpee is we feel sorry for you.
Basic Commands
Find
This will help you find files and/or folders. Its power is amazing. It is often used in conjunction with commands. First find something and then do something.
find directory -user username
Directory is relative unless you specify absolute
Example: This will find all files and directories in the current directory AND recursively. Then it will touch the file changing its mtime and for most computers that is the timestamp that is read. Nextcloud will not accept old timestamps and so we have to use this command to clean things up. Lots of great "find" stuff here.
find . -exec touch {} \;
for some more security you can
find . -execdir touch '{}' \;
-execdir runs each command from the directory in which the find result is located. [the curly brace pair] should be quoted (for example, '{}') to protect it from interpretation by shells".
Changing Ownership
There are many ways to do this. Half of them may be more clever.
sudo chown walt:walt /path/to/filename
This will make the file filename belong to the user walt and the group walt. Usually we change the group and user at the same time.
OR
sudo chown user:group /path/to/directory/
This changes only the directory to user and group specified
Adding the -R flag after the chown will recurse into directories and files.
sudo chown -R . . .
- We are not sure why the capital r is needed in some cases. But it is here.
Changing Permissions
This is very similar except we use chmod instead of chown
sudo chmod 644 /path/to/file or path/to/directory/
Adding the -R flag does the same as above.
Change all files recursively to 644
find /var/www/html/moodlecnx/ -type f -exec chmod 644 {} \;
Change all directories recursively to 755
find /var/www/html/moodlecnx/ -type d -exec chmod 755 {} \;
you may need a "sudo" in front of chmod depending on where you are working.
Make a text file of the filenames in a directory
find -maxdepth 1 -type f ! -name flist.txt -printf "%P\n" > flist.txt
Command information:
-maxdepth
: Don't search for files below folders of one level down.type f
: Search for only files-printf "%P\n"
: Print the names only and on separate lines> flist.txt
: Store those names (using output redirection) in a file to be created on the fly called `flist.txt! -name flist.txt
: Skips the name of the output file from the generated list
Make Directory
mkdir nameOfFolder
sudo mkdir name1 name2 nameX
mkdir -p /Path/newFolder
Mount and Unmount
iso files
sudo mkdir /mnt/iso
sudo mount -o loop Win10_2004_English_x64.iso /mnt/iso
Symlinks
This allows you to click on a folder or directory and actually end up somewhere else.
For example if a user can't figure out how to get to the Documents folder from Dolphin.ln -s /real/file/or/folder/path /path/to/link
The link can't already exist.
the path can be a file or directory.
Delete all files in folder tree
This will find all files under the parent directory(absolute) and recursively delete them.
It will not delete hidden files.
find ~/msgcnxFiles/Teacher/Science/Biology/For\ Upload/ -type f -delete
zip Multiple Files with Date
zip -r path/from/here/"trial-$(date +"%Y-%m-%d")" Folder1 Folder2 Folder3
-r recurse into directories
You are adding the date command into the filename.
Then list all the wanted folders.
Note: The command is relative. It knows the folder you are in. You can specify all paths relative to the current folder or start with a / and you now have absolute referencing.
Delete files older than a specified time
find /home/walt/.config/Nextcloud/logs -name '*.gz' -type f -mmin +240 -exec rm {} \;
The above command will delete all files older than 240 minutes
You can change to -mtime +7 for older than 7 days.
Printing
lp is the classic
lp filename will print the file to the default printer with the default settings
lp -n 3 /path/*.pdf
Much time has been invested in learning this.
Make sure you install the printer correctly or you will get strange errors.
Stay away from "driverless" if you can.
List the default destination
lpstat -d
List the available printers
lpstat -d -p
List all jobs
lpstat -o
Cancel all jobs and delete them.
cancel -a -x
Print Portrait two sided.
lp -o sides=two-sided-long-edge
3 copies of any pdf file in that path.
-P page-list Specifies pages to print in the document. The list can contain a list of numbers and ranges (#-#) separated by commas, e.g.,
"1,3-5,16". The page numbers refer to the output pages and not the document's original pages
Print a directory
find -name "*Test.*" -exec lp -o sides=two-sided-long-edge {} \;
Rename
- This function has many many options. We can only begin here.
- Below is an example where we prepend a 0 onto file names so that they order correctly on all OSes
- This works in the current working directory
rename -e 's/\d+/sprintf("%02d",$&)/e' -- *.jpg
Kill
- This function has many many options. We can only begin here.
Find the process id (pid) you want to kill and kill it.
kill -9 3567
If you know the name you can kill by name.
killall -9 vlc