File management: Difference between revisions

From Han Wiki
Jump to navigation Jump to search
mNo edit summary
Tags: Mobile edit Mobile web edit
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Making a samba share ==
= Finding or searching through files and folders =
* ''Last tested on Ubuntu 16.04 LTS (xenial) & Windows 10.0.10586''


at the end of <span class="shell">/etc/samba/smb.conf</span> add a share config.


<source lang="properties">
== Search for a string ==
[ed]
 
        comment = pistis - echo admin
A case-sensitive search through all of the files from current folder and below.
        path = /var/www/ea
 
        admin users = mhan
<syntaxhighlight lang="bash">
        read only = No
$ find . | xargs grep -s "mystring" | more
        create mask = 0755
</syntaxhighlight>
 
A ''case-insensitive'' search through all of the files from current folder and below.
 
<syntaxhighlight lang="bash">
$ find . | xargs grep -si "mystring" | more
</syntaxhighlight>
 
A ''case-insensitive'' search through all of the files from current folder and below, and truncate long search results.
 
<syntaxhighlight lang="bash">
$ find . | xargs grep -sioE ".{0,20}mystring.{0,20}" | more
</syntaxhighlight>
 
An alternative way to search for keywords.
 
<source lang="bash">
$ cat file.txt | grep word
$ grep -r -e word /etc"
</source>
</source>


then add the Samba account.
 
= Copy including hidden files =
 
* ''Last tested on Ubuntu 14.04.2 LTS (trusty)''


<source lang="bash">
<source lang="bash">
$ sudo smbpasswd -a mhan
$ cp -r folder1/. target/
</source>
</source>


then restart samba
 
= Delete files =
 
 
== Delete files recursively ==
 
* ''Lasted tested on Ubuntu 16.04.2 LTS (xenial)


<source lang="bash">
<source lang="bash">
$ sudo systemctl restart smbd.service nmbd.service
$ find . -type f -name "*.mp4" -exec rm {} \;
</source>
</source>


then on Windows execute the following. For the username, you may have to enter it in the form <span class="package">mhan@pistis</span> or <span class="package">pistis\mhan</span>. Enter the password you used when you added the Samba account for the user.


<source lang="doscon">
== Fastest way to delete a folder ==
C:\>net use Z: \\pistis\ea /savecred /persistent:yes
 
* {{testedon|2021-03-09|RHEL 8.3}}
 
<source lang="console">
$ mkdir empty
$ rsync -a --delete empty/ foldername
</source>
</source>




== Copy including hidden files ==
== Another method of deleting files under a folder ==
 
* ''Last tested on Ubuntu 14.04.2 LTS (trusty)''


<source lang="bash">
<source lang="console">
$ cp -r folder1/. target/
$ ls -f1 | xargs rm
</source>
</source>


= Setting a default group for a folder =


== Setting a default group for a folder ==
* ''Last tested on Ubuntu 16.04 LTS (xenial)''
* ''Last tested on Ubuntu 16.04 LTS (xenial)''


Line 73: Line 100:




= Unpacking gz files =
The files are compressed using Lempel-Ziv (LZ77) algorithm. GNU zip (gzip/gunzip) is used for this.
<syntaxhighlight lang="bash">$ gunzip file.gz</syntaxhighlight>
or
<syntaxhighlight lang="bash">$ gzip -d file.gz</syntaxhighlight>
= Zip a folder =
<source lang="bash">
$ zip -r test.zip ./test
</source>
= Mirror a folder =
{{testedon|2020-06-08|CentOS 8}}
Copy ALL and keep EVERYTHING same without deleting the source, but deleting files on the target folder that are not found in the source.
<source lang="console">
$ rsync --delete -HAXavr /source/folder/foldername/ /target/folder/foldername/
</source>
* ''Last tested on Ubuntu 16.04 LTS (xenial)''
This also works between servers. The first one is on the same server.
Explanation of parameters used:
* -a: archive - preserve permissions, ownership, and timestamps
* -v: verbose
* -z: compress
<syntaxhighlight lang="bash" highlight="1">
$ rsync -azv /folder_a/ /folder_b/
</syntaxhighlight>
This one is between different servers.
<syntaxhighlight lang="bash" highlight="1">
$ rsync -azv ~/folder_a/ mhan@tom.myserver.com:~/folder_b/
</syntaxhighlight>
= Move files =
<syntaxhighlight lang="bash" highlight="1">
$ rsync --remove-source-files -HAXzvhr /source/chanbara/* ./chanbara/
</syntaxhighlight>
= Move files older or less than x days =
* ''Last tested on Ubuntu 14.04.5 LTS (trusty)''
Make a list of files to copy.  Example here is a list of files older than 365 days.
<syntaxhighlight lang="bash" highlight="1">
$ find . -type f -mtime +365 > /tmp/rsyncfiles
</syntaxhighlight>
This is a list of files younger than 365 days
<syntaxhighlight lang="bash" highlight="1">
$ find . -type f -mtime -365 > /tmp/rsyncfiles
</syntaxhighlight>
rsync across the network to another server. This command deletes the source files after they are moved.
<syntaxhighlight lang="bash" highlight="1">
$ rsync --remove-source-files -zvh --files-from=/tmp/rsyncfiles . mhan@zinc.wherever.com:/target/folder/
</syntaxhighlight>


[[Category:System administration]]
[[Category:System administration]]

Latest revision as of 16:19, 11 April 2024

Finding or searching through files and folders

Search for a string

A case-sensitive search through all of the files from current folder and below.

$ find . | xargs grep -s "mystring" | more

A case-insensitive search through all of the files from current folder and below.

$ find . | xargs grep -si "mystring" | more

A case-insensitive search through all of the files from current folder and below, and truncate long search results.

$ find . | xargs grep -sioE ".{0,20}mystring.{0,20}" | more

An alternative way to search for keywords.

$ cat file.txt | grep word
$ grep -r -e word /etc"


Copy including hidden files

  • Last tested on Ubuntu 14.04.2 LTS (trusty)
$ cp -r folder1/. target/


Delete files

Delete files recursively

  • Lasted tested on Ubuntu 16.04.2 LTS (xenial)
$ find . -type f -name "*.mp4" -exec rm {} \;


Fastest way to delete a folder

  • Last tested on RHEL 8.3 (2021-03-09)
$ mkdir empty
$ rsync -a --delete empty/ foldername


Another method of deleting files under a folder

$ ls -f1 | xargs rm

Setting a default group for a folder

  • Last tested on Ubuntu 16.04 LTS (xenial)

This is to set permission for an existing folder for collaboration. This assumes the group name to be dev, and the folder to be /srv/www/project.

This sets setgid bit on the folder.

$ sudo find /srv/www/project -type d -exec chgrp dev {} +
$ sudo find /srv/www/project -type d -exec chmod g+s {} +

Make it writable by the group.

$ sudo chmod -R g+w /srv/www/project

Change group ownership of existing files.

$ sudo chown -R mhan:dev /srv/www/project

These commands should be executed for a new folder.

$ sudo chgrp dev /srv/www/project
$ sudo chmod g+s /srv/www/project


Unpacking gz files

The files are compressed using Lempel-Ziv (LZ77) algorithm. GNU zip (gzip/gunzip) is used for this.

$ gunzip file.gz

or

$ gzip -d file.gz


Zip a folder

$ zip -r test.zip ./test


Mirror a folder

  • Last tested on CentOS 8 (2020-06-08)

Copy ALL and keep EVERYTHING same without deleting the source, but deleting files on the target folder that are not found in the source.

$ rsync --delete -HAXavr /source/folder/foldername/ /target/folder/foldername/
  • Last tested on Ubuntu 16.04 LTS (xenial)

This also works between servers. The first one is on the same server.

Explanation of parameters used:

  • -a: archive - preserve permissions, ownership, and timestamps
  • -v: verbose
  • -z: compress
$ rsync -azv /folder_a/ /folder_b/

This one is between different servers.

$ rsync -azv ~/folder_a/ mhan@tom.myserver.com:~/folder_b/


Move files

$ rsync --remove-source-files -HAXzvhr /source/chanbara/* ./chanbara/

Move files older or less than x days

  • Last tested on Ubuntu 14.04.5 LTS (trusty)

Make a list of files to copy. Example here is a list of files older than 365 days.

$ find . -type f -mtime +365 > /tmp/rsyncfiles

This is a list of files younger than 365 days

$ find . -type f -mtime -365 > /tmp/rsyncfiles

rsync across the network to another server. This command deletes the source files after they are moved.

$ rsync --remove-source-files -zvh --files-from=/tmp/rsyncfiles . mhan@zinc.wherever.com:/target/folder/